[leetcode] 22. Generate Parentheses
题目大意
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相关推荐
- [LeetCode] #22 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
- LeetCode 22. Generate Parentheses--Python 解法--广度优先、深度优先解法
题目地址:Generate Parentheses - LeetCode Given n pairs of parentheses, write a function to generate all ...
- 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- [DFS|剪枝] leetcode 22 括号生成
[DFS|剪枝] leetcode 22 括号生成 1.题目 题目链接 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且有效的括号组合. 示例: 输入:n = 3 输出:[& ...
- LeetCode 22. 括号生成(Generate Parentheses)
题目描述 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: ["((()))","(()() ...
最新文章
- 多路径配置udev_ASM磁盘多路径及udev配置
- redistemplate 设置永不过期_密码已过期 拒绝访问如何处理?
- C++ 对二维数组排序 升序 降序
- bt5重启网卡命令_BackTrack 5 简单网络配置命令
- python入门-分类和回归各种初级算法
- C语言(从入门到精通)
- C语言入门之鸡兔同笼(简易版)
- MATLAB图像处理(一)——计算机图形学之图像形状识别
- 计算机安全模式无法启动,电脑无法启动,电脑安全模式进不去解决方法大全?...
- php 把数字大写,php 阿拉伯数字转中文大写金额
- APM的解锁(ARM)流程
- 64位系统装32位mysql有问题吗_32位系统部署到64位下常见问题及解决
- 空间分析方法在计算机上的应用,空间分析
- oracle12c用plsql连不上,PLSQL连接oracle12c
- 《PNG文件格式》(二)PNG文件格式分析
- meta20 无法安装 google play_【黑科技】安卓手机安装Google Play
- UVA 12235 Help Bubu
- “新基建”拉开时空大数据新需求,AI 地图破解数字底座密码
- java 生成mysql dto_如何根据动态SQL代码自动生成DTO
- 新手必看 如何做好跨境电商
热门文章
- 关于python的一些好的书籍推荐-如果只能推荐3本关于python的书,你会推荐哪3本?...
- python编程从入门到实践看完了看什么-小白Python编程从入门到实践——列表是什么...
- 用python绘制漂亮的图形-python如何画出漂亮的地图?
- python入门教程书籍-初学者最好的Python书籍
- python3各版本区别-Python2 与Python3的版本区别实例分析
- python工作招聘-学习Python 能找到工作?1300+条招聘信息告诉你答案
- python绘制灰度图片直方图-python – numpy图像中灰度值的直方图
- python经典案例-Python3经典100例(①)
- python批量下载网页文件-python使用selenium实现批量文件下载
- python中文解释-python注释和2版本的中文乱码