给定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相关推荐

  1. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  2. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  3. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

  4. LeetCode:Generate Parentheses

    题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  5. LeetCode Generate Parentheses

    原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...

  6. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. 【LeetCode从零单排】No22.Generate Parentheses

    题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  8. Generate Parentheses

    描述 Given n pairs of parentheses, write a function to generate all combinations of wellformed parenth ...

  9. 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

最新文章

  1. Appium学习笔记2_Android获取元素篇
  2. 导师发现我刷短视频,给我发了一条链接
  3. 看懂mysql执行计划--官方文档
  4. 深入学习 History 对象管理浏览器会话历史
  5. html标签书写注意事项,HTML一些标签注意事项
  6. 什么样的征信才算是好的?
  7. 货车运输(洛谷P1967)(倍增)
  8. python钉钉机器人发送消息_python调用钉钉机器人发送消息
  9. Tree(树链剖分+线段树延迟标记)
  10. Linux命令解释之rm
  11. php怎么阻止页面跳转,php如何控制页面跳转
  12. android画布橡皮,Android 图片涂鸦橡皮擦功能
  13. java安装_Java开发人员应该知道的7种新工具
  14. 前端隐秘角落 - web安全的演变历程
  15. cati服务器授权信息无效,cati安装
  16. Quantum Espresso Hands-On实战训练(五)——电荷密度可视化
  17. 华东师范大学 计算机 博士 毕业论文,华东师大:1/4博士生完不成论文难毕业
  18. RF射频信号,高速信号能将电源平面作为参考平面吗?
  19. H5如何拉起微信扫一扫
  20. 根据所选日期、当前日期计算一周的日期和星期,农历

热门文章

  1. java宏定义_现代化的 Java (二十六)—— Akka Stream Graph
  2. c语言如何用双重循环去重,c语言中一个一维数组怎样去重?
  3. mac android studio 打不开adb,Android-Macbook ADB无法打开
  4. java constant 类_如何从Constant java为注释提供值
  5. php 自旋锁,Linux内核自旋锁使用笔记
  6. python的分支结构
  7. 使用H5实现机器人脸
  8. 协议处理程序初始化失败_TCP协议、算法和原理
  9. python基础期末考试_python基础试题(4)
  10. 在jupyter界面误删了jupyter notebook