lintcode -- 生成括号
来源:互联网 发布:衢州软件开发 编辑:程序博客网 时间:2024/05/17 08:18
给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。
给定 n = 3
, 可生成的组合如下:
"((()))", "(()())", "(())()", "()(())", "()()()"
/*
采用递归树的思想,当左括号数大于右括号数时可以加左或者右括号,
否则只能加左括号,当左括号数达到n时,剩下全部
采用递归树的思想
left: 左括号的数量
right:右括号数量
n:括号的对数
当left == n:表示左括号已经到达最大值了,只能添加右括号
当left >= right: 表示左括号数量 小于 右括号数量,可以添加左括号 也可以添加右括号
当left < right or left >n or right >n : 非法操作
*/
public class Solution {
public ArrayList<String> generateParenthesis(int n){
ArrayList<String> res = new ArrayList<String>();
if(n<=0)return res;
String paren = "";
helper(res,paren,n,n);
return res;
}
public void helper(ArrayList<String> res,String paren,int left,int right){
if(left == 0 && right == 0){
res.add(paren);
return;
}
if(left > 0){
helper(res,paren+"(",left -1,right);
}
if(right > 0 && left < right){
helper(res,paren+")",left,right -1 );
}
}
}
- LintCode : 生成括号
- LintCode-生成括号
- LintCode:生成括号
- LintCode:生成括号
- lintcode,生成括号
- 生成括号-lintcode
- lintcode -- 生成括号
- LintCode 生成括号
- DFS-lintcode生成括号
- 生成括号-LintCode
- lintcode刷题--生成括号
- lintcode--生成括号
- [Lintcode]Generate Parentheses 生成括号
- Generate Parentheses 生成括号-- LintCode题解
- lintcode括号匹配
- 生成括号
- 生成括号
- 生成括号
- IMX6Q下tlv320aic3x音频驱动移植
- IDA权威指南阅读笔记2
- Python Linux上安装MYSQLdb
- Cocoa与Cocoa Touch区别
- 使用tomcat产生的java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
- lintcode -- 生成括号
- POJ 3613 Cow Relays k步最短路 (floyd + 矩阵快速幂)
- 《人人都是产品经理》——第六章笔记
- C++之操作符重载
- 产生随机数的Random类的框架
- 蝉
- scandir() has been disabled for security reasons
- 使用PostgreSql心得
- Vuforia--VirtualButton的使用