题目大意

https://leetcode.com/problems/generate-parentheses/description/

22. Generate Parentheses

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

For example, given n = 3, a solution set is:

["((()))","(()())","(())()","()(())","()()()"
]

解题思路

The idea here is to only add '(' and ')' that we know will guarantee us a solution (instead of adding 1 too many close). Once we add a '(' we will then discard it and try a ')' which can only close a valid '('. Each of these steps are recursively called.

伪代码(递归法)

if len(s) == 2*n:将s加入到输出列表
if 左括号数目 < n:加入左括号
if 右括号数目 < 左括号数目:加入右括号

Java解法

 public List<String> generateParenthesis(int n) {List<String> list = new ArrayList<String>();backtrack(list, "", 0, 0, n);return list;}public void backtrack(List<String> list, String str, int open, int close, int max){if(str.length() == max*2){list.add(str);return;}if(open < max)backtrack(list, str+"(", open+1, close, max);if(close < open)backtrack(list, str+")", open, close+1, max);}

Python解法

class Solution(object):def generateParenthesis(self, n):""":type n: int:rtype: List[str]"""self.output_list = list()self.n = ndef backtrack(s, left, right):if len(s) == n * 2:self.output_list.append(s)returnif left < self.n:backtrack(s + "(", left + 1, right)if right < left:backtrack(s + ")", left, right + 1)backtrack("", 0, 0)return self.output_list

参考:https://leetcode.com/problems/generate-parentheses/discuss/10100/Easy-to-understand-Java-backtracking-solution

转载于:https://www.cnblogs.com/bymo/p/9644896.html

[leetcode] 22. Generate Parentheses相关推荐

  1. [LeetCode] #22 Generate Parentheses

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

  2. LeetCode 22. Generate Parentheses

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

  3. [leetcode] 22. Generate Parentheses(medium)

    原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...

  4. LeetCode 22. Generate Parentheses--Python 解法--广度优先、深度优先解法

    题目地址:Generate Parentheses - LeetCode Given n pairs of parentheses, write a function to generate all ...

  5. 22. Generate Parentheses

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

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

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

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

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

  8. [DFS|剪枝] leetcode 22 括号生成

    [DFS|剪枝] leetcode 22 括号生成 1.题目 题目链接 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合. 示例: 输入:n = 3 输出:[& ...

  9. LeetCode 22. 括号生成(Generate Parentheses)

    题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: ["((()))","(()() ...

最新文章

  1. 多路径配置udev_ASM磁盘多路径及udev配置
  2. redistemplate 设置永不过期_密码已过期 拒绝访问如何处理?
  3. C++ 对二维数组排序 升序 降序
  4. bt5重启网卡命令_BackTrack 5 简单网络配置命令
  5. python入门-分类和回归各种初级算法
  6. C语言(从入门到精通)
  7. C语言入门之鸡兔同笼(简易版)
  8. MATLAB图像处理(一)——计算机图形学之图像形状识别
  9. 计算机安全模式无法启动,电脑无法启动,电脑安全模式进不去解决方法大全?...
  10. php 把数字大写,php 阿拉伯数字转中文大写金额
  11. APM的解锁(ARM)流程
  12. 64位系统装32位mysql有问题吗_32位系统部署到64位下常见问题及解决
  13. 空间分析方法在计算机上的应用,空间分析
  14. oracle12c用plsql连不上,PLSQL连接oracle12c
  15. 《PNG文件格式》(二)PNG文件格式分析
  16. meta20 无法安装 google play_【黑科技】安卓手机安装Google Play
  17. UVA 12235 Help Bubu
  18. “新基建”拉开时空大数据新需求,AI 地图破解数字底座密码
  19. java 生成mysql dto_如何根据动态SQL代码自动生成DTO
  20. 新手必看 如何做好跨境电商

热门文章

  1. 关于python的一些好的书籍推荐-如果只能推荐3本关于python的书,你会推荐哪3本?...
  2. python编程从入门到实践看完了看什么-小白Python编程从入门到实践——列表是什么...
  3. 用python绘制漂亮的图形-python如何画出漂亮的地图?
  4. python入门教程书籍-初学者最好的Python书籍
  5. python3各版本区别-Python2 与Python3的版本区别实例分析
  6. python工作招聘-学习Python 能找到工作?1300+条招聘信息告诉你答案
  7. python绘制灰度图片直方图-python – numpy图像中灰度值的直方图
  8. python经典案例-Python3经典100例(①)
  9. python批量下载网页文件-python使用selenium实现批量文件下载
  10. python中文解释-python注释和2版本的中文乱码