[LeetCode]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:
- [
- "((()))",
- "(()())",
- "(())()",
- "()(())",
- "()()()"
- ]
- 根据题目要求计算出所有可能性,我们使用回溯来处理,首先确定几个问题
1.必须先有左括号才能有右括号,不然序列不合法
2.左括号的个数必须小于给出的个数n
基于上面的条件,我们设计回溯的时候需要先放左括号,在有左括号的基础上才能有右括号,所以我们设置2个left和right来计算左右括号的个数
left<n时可以放置左括号,然后递归进去处理,当left=n时放右括号,right<left,这个条件很重要,如果没有的话可能导致在第1个位置left=0时放置右括号,这是不合法序列
最后list.length==2*n时返回
- class Solution {
- public List<String> generateParenthesis(int n) {
- List<String> ans = new ArrayList();
- backtrack(ans, "", 0, 0, n);
- return ans;
- }
- public void backtrack(List<String> ans, String cur, int open, int close, int max){
- if (cur.length() == max * 2) {
- ans.add(cur);
- return;
- }
- if (open < max)
- backtrack(ans, cur+"(", open+1, close, max);
- if (close < open)
- backtrack(ans, cur+")", open, close+1, max);
- }
- }
回溯就类似往栈里放东西,先进后出,所以所有可能性的个数就是一个卡特兰数,时间复杂度也就是这个卡特兰数
[LeetCode]22. Generate Parentheses括号生成的更多相关文章
- 【LeetCode】22. Generate Parentheses 括号生成
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:括号, 括号生成,题解,leetcode, 力扣,Pyt ...
- [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(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [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 ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
随机推荐
- [转载]ssget 用法详解 by yxp
总结得很好的ssget用法.....如此好文,必须转载. 原文地址: http://blog.csdn.net/yxp_xa/article/details/72229202 ssget 用法详解 b ...
- 【FAQ】服务下线
原因:磁盘已满
- PyCharm4.5.4注册码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 name : newasp == ...
- 降维之主成分分析法(PCA)
一.主成分分析法的思想 我们在研究某些问题时,需要处理带有很多变量的数据,比如研究房价的影响因素,需要考虑的变量有物价水平.土地价格.利率.就业率.城市化率等.变量和数据很多,但是可能存在噪音和冗余, ...
- 关于运行robot framework 报错解决方法,ModuleNotFoundError: No module named 'robot'
报错: command: pybot.bat --argumentfile c:\users\76776\appdata\local\temp\RIDEiw0utf.d\argfile.txt --l ...
- js 三大家族之offset
JS中的offset家族: 一.offsetWidth与offsetHeight: 获取的是元素的实际宽高 = width + border + padding 注意点: 1.可以获取行内及内嵌的宽高 ...
- C++_类入门1-对象和类的介绍
面向对象是(OOP)是特殊的.设计程序的概念性方法:包含以下特性: 抽象: 封装和数据隐藏: 多态: 继承: 代码的可重用性: 为了实现这些特性并且将这些特性组合在一起,C++所做的最重要的改进是提供 ...
- plot over time
先选择监测点 最后输出,由于所有数据都被输出,因此需要等待久一点 可以勾选需要的值,记得更换勾选变量后再次点击apply 最后的效果: 最后可以把数据写出来做后处理 输出后的数据:
- [水题AC乐] - 贪心
HDU - 1009 https://paste.ubuntu.com/p/rgSYpSKkwW/ POJ - 1017 麻烦的模拟 贪心 题意就是用尽量少的66h箱子装nnh的物品,贪心策略很明显, ...
- PHP_$_SERVER中QUERY_STRING,REQUEST_URI的用法
$_SERVER存储当前服务器信息,其中有几个值如 $_SERVER["QUERY_STRING"], $_SERVER["REQUEST_URI"], $_S ...