36/100. Generate Parentheses
给定n对括号,要求输出所有合法的括号排列。
使用DFS算法,主要分为两种情况:
①若有剩余的“(”
,则添加“(”
后继续递归;
②若有剩余的“)”
,且“)”
的个数多于“(”
,即为排好的“(”
个数多于“)”
,这是判定是否合法的条件,则添加“)”
后继续递归。
class Solution(object):def __init__(self):self.res = []def generateParenthesis(self, n):""":type n: int:rtype: List[str]"""self.helper("", n, n) #n个左括号,n个右括号return self.resdef helper(self, path, left, right):if left == 0 and right == 0:self.res.append(path)if left > 0:self.helper(path+"(", left-1, right)if right > 0 and right > left: self.helper(path+")", left, right-1)
36/100. Generate Parentheses相关推荐
- [leetcode] 22. Generate Parentheses
题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...
- LeetCode:Generate Parentheses
题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- LeetCode Generate Parentheses
原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- Generate Parentheses
描述 Given n pairs of parentheses, write a function to generate all combinations of wellformed parenth ...
- 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
最新文章
- Appium学习笔记2_Android获取元素篇
- 导师发现我刷短视频,给我发了一条链接
- 看懂mysql执行计划--官方文档
- 深入学习 History 对象管理浏览器会话历史
- html标签书写注意事项,HTML一些标签注意事项
- 什么样的征信才算是好的?
- 货车运输(洛谷P1967)(倍增)
- python钉钉机器人发送消息_python调用钉钉机器人发送消息
- Tree(树链剖分+线段树延迟标记)
- Linux命令解释之rm
- php怎么阻止页面跳转,php如何控制页面跳转
- android画布橡皮,Android 图片涂鸦橡皮擦功能
- java安装_Java开发人员应该知道的7种新工具
- 前端隐秘角落 - web安全的演变历程
- cati服务器授权信息无效,cati安装
- Quantum Espresso Hands-On实战训练(五)——电荷密度可视化
- 华东师范大学 计算机 博士 毕业论文,华东师大:1/4博士生完不成论文难毕业
- RF射频信号,高速信号能将电源平面作为参考平面吗?
- H5如何拉起微信扫一扫
- 根据所选日期、当前日期计算一周的日期和星期,农历
热门文章
- java宏定义_现代化的 Java (二十六)—— Akka Stream Graph
- c语言如何用双重循环去重,c语言中一个一维数组怎样去重?
- mac android studio 打不开adb,Android-Macbook ADB无法打开
- java constant 类_如何从Constant java为注释提供值
- php 自旋锁,Linux内核自旋锁使用笔记
- python的分支结构
- 使用H5实现机器人脸
- 协议处理程序初始化失败_TCP协议、算法和原理
- python基础期末考试_python基础试题(4)
- 在jupyter界面误删了jupyter notebook