题目描述:

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

解题思路:

利用递归的思想。用left和right分别记录剩余的左,右括号数。

每次都首先把左括号放进字符串中。

放入右括号的条件是:右括号剩余的数目大于0,且左括号剩余数目小于右括号剩余数目。

当left和right都为0时表明字符串已形成,加入到List中。

代码如下:

public class Solution {
List<String> list = new ArrayList<String>();
public List<String> generateParenthesis(int n) {
if (n <= 0)
return list;
generate("", n, n);
return list;
}
public void generate(String s, int left, int right) {
if (left == 0 && right == 0) {
list.add(s);
return;
}
if (left > 0)
generate(s + '(', left - 1, right);
if (right > 0 && right > left)
generate(s + ')', left, right - 1);
}
}

Java [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 (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  3. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

  4. LeetCode 22. Generate Parentheses

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

  5. [leetcode]22. Generate Parentheses生成括号

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

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

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

  7. [LeetCode] 22. Generate Parentheses ☆☆

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

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

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

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

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

随机推荐

  1. 解决 ListView 水平滚动条不出现的问题(转载)

    问题的原因:http://www.cnblogs.com/nankezhishi/archive/2010/03/17/wpfbug13.html 解决方法:http://www.cnblogs.co ...

  2. 【转】用perl写的单位电脑信息采集程序

    perl,后来我又改过了增加了一些交互和数据库检测的功能.主要用于收集ip.mac.姓名.房间,后来又加入了维修记录的功能.服务器端接受数据并存入数据库中. 代码如下: 主要用于收集ip.mac.姓名 ...

  3. Linux配置Tomcat(转载)

    转载自:http://www.cnblogs.com/zhoulf/archive/2013/02/04/2891633.html 安装说明 安装环境:CentOS-6.3安装方式:源码安装 软件:a ...

  4. kissy使用注意事项

    使用kissy注意: 下载kissy压缩包后只需要拷贝build包到项目中即可, 样式自己写不用引用. 1.4.x引这个文件seed-min.js1.3.x引kissy-min.js

  5. First Groovy

    class Sample { def names = ["anna", "annie", "tommy", "bobby" ...

  6. 1197: [HNOI2006]花仙子的魔法 - BZOJ

    Description Input 包含两个整数,并用一个空格隔开,第一个整数表示实施魔法的次数m,第二个整数表示空间的维数n.其中,1≤m≤100,1≤n≤15. Output 仅包含一个整数,表示 ...

  7. 团体程序设计天梯赛-练习集L2-001. 紧急救援(dijkstra)

    L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...

  8. The 5th Zhejiang Provincial Collegiate Programming Contest---ProblemF:Faster, Higher, Stronger

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2970 唔...第二水题.除了Faster是输出最小外,另外两个都是输出最大 ...

  9. 自助Linux之问题诊断工具strace

    转  http://www.cnblogs.com/bangerlee/archive/2012/02/20/2356818.html 引言 “Oops,系统挂死了..." “Oops,程序 ...

  10. uva 125

    floyd 算法   如果存在无数条路  则存在a->a的路  a->b的路径数等于 a->i 和 i->b(0=<i<=_max) 路径数的乘积和 #includ ...