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:

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

题意:  给定一个长度,生成所有此长度的合法括号序列。

Solution1:Backtracking.

We can observe that two constraints:

(1)  left Parentheses should come faster ahead to reach given n.

(2) in each level, we increment left Parentheses count (or right Parentheses count) from given n, and add '(' (or ')') into path

At last, left and right Parentheses count become 0, we can gerenate a result path.

code:

 /*
Time: O(2^n).
Space: O(n). use O(n) space to store the sequence(path)
*/
class Solution {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList<>();
if (n == 0) return result;
helper("", n, n, result);
return result; } private void helper(String path, int left, int right, List<String> result) {
// corner
if (left > right) return;
// base
if (left == 0 && right == 0) {
result.add(path);
return;
}
if (left > 0) {
helper(path + "(", left - 1, right, result);
}
if (right > 0) {
helper(path + ")", left, right - 1, result);
}
}
}

[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. [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. 22.Generate Parentheses[M]括号生成

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

  4. generate parentheses(生成括号)

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

  5. [LintCode] Generate Parentheses 生成括号

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

  6. [LeetCode]22. Generate Parentheses括号生成

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

  7. [LeetCode] Generate Parentheses 生成括号

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

  8. 22. Generate Parentheses生成指定个括号

    生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...

  9. LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)

    题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description   给一个整数n,找到所有合法的 () pairs ...

随机推荐

  1. Spring各个jar包作用

    Spring AOP:Spring的面向切面编程,提供AOP(面向切面编程)的实现Spring Aspects:Spring提供的对AspectJ框架的整合Spring Beans:Spring IO ...

  2. py-day3-4 python 匿名函数

    # 匿名函数 lamdba name = 'xiaoma' f = lambda x:x+'jun' res = f(name) print('匿名函数的运行结果:',res) 匿名函数的运行结果: ...

  3. MySQL查询不使用索引汇总 + 如何优化sql语句

    不使用索引原文 : http://itlab.idcquan.com/linux/MYSQL/918330.html MySQL查询不使用索引汇总 众所周知,增加索引是提高查询速度的有效途径,但是很多 ...

  4. IIS 7.0的集成模式和经典模式

    IIS7.0中的Web应用程序有两种配置模式:经典模式和集成模式.经典模式是为了与之前的版本兼容,使用ISAPI扩展来调用ASP.NET运行库, 原先运行于IIS6.0下的Web应用程序迁移到IIS7 ...

  5. ArrayList add方法(转)

    由于 BrowerList 输出结果都是最后一条记录,后来网上查到了 if (dRead.HasRows) { List<Class_RejectQuery> BrowerList = n ...

  6. JAVA 异常类型结构分析

    JAVA 异常类型结构分析 Throwable 是所有异常类型的基类,Throwable 下一层分为两个分支,Error 和 Exception. Error 和 Exception Error Er ...

  7. postgresql模糊查询json类型字段内某一属性值

    需求场景: 目录以jsonb格式存储在数据库表t的chapter字段中,需要菜单路径中包含指定字符串(比如“语文”或者“上学期”)的menu 以下为chapter字段存储json示例: { " ...

  8. sql server driver ODBC驱动超时

  9. 网页编程工具:EditPlus

    字体:Consolas EditPlus,很土很简单很强大的网页编程工具 http://www.editplus.com/download.html  下载 http://www.cnblogs.co ...

  10. centos密码策略

    centos7密码策略 https://blog.csdn.net/qq_36896749/article/details/80264280 centos7设置密码规则 https://blog.cs ...