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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]
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的更多相关文章
- [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/ Given n pairs of parentheses, write a function t ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 蜗牛慢慢爬 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 ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
- [leetcode] 22. Generate Parentheses(medium)
原题 思路: 利用DFS,搜索每一种情况,同时先加"("后加")",保证()匹配正确. 最近开始学习前端,尝试用js来写. const generate = f ...
随机推荐
- mysql 创建存储过程报错
在创建存储过程前把结束符定义为 delimiter // 然后再创建就不会报错
- 在Python中使用可变长参数列表
函数定义 在函数定义中使用*args和**kwargs传递可变长参数. *args用作传递非命名键值可变长参数列表(位置参数); **kwargs用作传递键值可变长参数列表 函数调用 在调用函数时,使 ...
- subtext3插件安装
1.启用Package Control,利用它进行插件安装: 启用方法:菜单栏-view-show console 在弹出的输入框内,输入以下代码,按回车,稍后会出现package control安装 ...
- C++构造函数2
一.构造函数分类 普通构造函数,复制(拷贝)构造函数,赋值构造函数, #include <iostream> using namespace std; class A { public: ...
- EntityFramework 优化建议
Entity Framework目前最新版本是6.1.3,当然Entity Framework 7 目前还是预览版,并不能投入正式生产环境,估计正式版16年第一季度会出来,了解过EF7的部分新特性后, ...
- R自动数据收集第一章概述——《List of World Heritage in Danger》
导包 library(stringr) library(XML) library(maps) heritage_parsed <- htmlParse("http://en ...
- prototype,__proto__,constructor
proto属性: 所有对象都有此属性.但它不是规范里定义的属性,并不是所有JavaScript运行环境都支持.它指向对象的原型,也就是你说的继承链里的原型.通过Object.getPrototypeO ...
- centos7 编译php56
编译安装php5.6 centos7环境 步骤: //下载php5.6 wget http://cn2.php.net/distributions/php-5.6.26.tar.bz2 //安装依赖 ...
- json 使用 (下)
使用JSON JSON也就是JavaScript Object Notation,是一个描述数据的轻量级语法.JSON的优雅是因为它是JavaScript语言的一个子集.接下来你将看到它为什么如此重要 ...
- VMware ubuntu中执行python文件的操作小结