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. PythonStudy——格式化输入小练习

    # 练习:用户输入姓名.年龄.工作.爱好 ,然后打印成以下格式# ------------ info of Egon -----------# Name : Egon# Age : 22# Sex : ...

  2. C++类中一个构造函数调用另一个构造函数

    class A { int a; int b; int c; public: A(int aa, int bb) : a(aa), b(bb),c(0) { cout << "a ...

  3. 《Linux内核原理与分析》第四次作业

    跟踪分析Linux内核的启动过程 使用实验楼的虚拟机打开shell 使用 gdb 跟踪调试内核 使用 qemu qemu -kernel linux-3.18.6 /arch/x86/boot/baI ...

  4. Jenkins入门-环境搭建(1)

    因为Jenkins的环境搭建比较简单,本来不想来介绍,但是发现有些入门小朋友,从各种网站上下载的各种安装包来搭建,最后导致出现了各种千奇百怪的问题,介于这种情况下我决定还是来写一下Jenkins的环境 ...

  5. jekins构建触发器详解-日程表的使用

    日程表参数解释如下: 第一个参数代表的是分钟 minute,取值 0~59: 第二个参数代表的是小时 hour,取值 0~23: 第三个参数代表的是天 day,取值 1~31: 第四个参数代表的是月 ...

  6. 常量&字符编码

    day1 name='Nod Chen' name2=name print('My name is ',name,name2) name='Luna zhou' print(name,name2) _ ...

  7. 网络之TCP握手总结

    目录: 31.Tcp握手的一些问题? 21.Tcp三次握手及SYN攻击: 四次握手? 为什么建立连接是三次握手,而关闭连接却是四次挥手? 13.TCP释放连接四次握手 12.TCP建立连接三次握手 1 ...

  8. docker network基础

    前面介绍了nginx与php两个容器间是如何进行通信的: [root@docker ~]# docker run -d --name=php -v /www:/usr/local/nginx/html ...

  9. django之COOKIE 与 SESSION

    COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie ...

  10. HTML---仿网易新闻登录页

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...