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:

[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
public class Solution {
private List<String> list = new ArrayList<String>(); public List<String> generateParenthesis(int n) {
run("", n, 0);
return list;
} // l 为剩余左括号数,r为剩余未匹配的右括号数目
public void run(String str, int l, int r) {
if (l == 0 && r == 0) {
list.add(str);
return; //跳出run方法
}
if (l > 0) {
String s = str + "(";
run(s, l-1, r+1);
}
if (r > 0) {
String s = str + ")";
run(s, l, r-1);
}
}
}

程序结果:

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. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  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 [Difficulty: Medium]

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

  6. [LeetCode] 22. Generate Parentheses ☆☆

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

  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(找到所有匹配的括号组合)

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

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

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

随机推荐

  1. 如何使用DOS命令(cd命令)

    如何使用DOS命令 DOS是Disk Operation System(磁盘操作系统)的简称,是个人计算机上的一类操作系统.它直接操纵管理硬盘的文件,一般都是黑底白色文字的界面.顾名思义,DOS主要是 ...

  2. easyUI时间控件 使用

    1,时间格式化,分隔符为     “—” // 日期格式化 function myformatter(date){ var y = date.getFullYear(); var m = date.g ...

  3. opencart邮件模板

    在opencart中,几乎所有的版权信息都写在language里,我们找到catalog/language/your language/mail/order.php这个语言文件,找到 $_['text ...

  4. Linux平台开发指南

    声明:以下内容摘自http://www.me115.com/post/25.html 以下技术和工具是Linux平台下工作的基础,熟练掌握: C++ 工作语言,重要性不言而喻: 入门: <C++ ...

  5. centos7 编译php56

    编译安装php5.6 centos7环境 步骤: //下载php5.6 wget http://cn2.php.net/distributions/php-5.6.26.tar.bz2 //安装依赖 ...

  6. PHP错误以及异常处理

    以前一直觉得php的异常处理没有什么,现在才发现这个还真是门学问,于是狠下心来好好研究了一下,写一篇文章,也作备忘吧. 1. php错误 无论是什么语言编程,都会有如下三种错误,当然php也不例外. ...

  7. MyBatis处理一行数据-MyBatis使用sum语句报错-MyBatis字段映射-遁地龙卷风

    第二版 (-1)写在前面 我用的是MyBatis 3.2.4 (0) 编程轶事 select sum(value) ,sum(value2)  from integral_list where  Me ...

  8. iOS 25个性能优化/内存优化常用方法

    1. 用ARC管理内存 ARC(Automatic ReferenceCounting, 自动引用计数)和iOS5一起发布,它避免了最常见的也就是经常是由于我们忘记释放内存所造成的内存泄露.它自动为你 ...

  9. Python自动化之模板继承和cookie

    request请求头信息 type(request) //查看类 from django.core.handlers.wsgi import WSGIRequest 结果会以字典的形式存在 reque ...

  10. Android安全相关文章[不定期更新…]

    http://drops.wooyun.org/papers/2893 Intent scheme URL attack http://drops.wooyun.org/tips/3812 Andro ...