生成括号

给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。

样例

给定 n = 3, 可生成的组合如下:

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

解题

参考链接

采用递归树的思想

left: 左括号的数量

right:右括号数量

n:括号的对数

当left == n:表示左括号已经到达最大值了,只能添加右括号

当left >= right: 表示左括号数量 小于 右括号数量,可以添加左括号 也可以添加右括号

当left < right or left >n or right >n : 非法操作

public class Solution {
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
public ArrayList<String> generateParenthesis(int n) {
// Write your code here
ArrayList<String> result = new ArrayList<String>();
if(n<=0)
return result;
int left = 0,right=0;
String str="";
generateParenthesis(n,left,right,str,result);
return result;
}
public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
if(left >n || right >n || left < right)
return;
if(left == n){
for(int i=0;i< n- right;i++)
str+=")";
result.add(str);
return;
} // left>=right
generateParenthesis(n, left+1, right,str+"(",result);
generateParenthesis(n, left, right+1,str+")",result); }
}

下面的程序参考上面博客链接中,感觉判断添加是不是少了。

public class Solution {
/**
* @param n n pairs
* @return All combinations of well-formed parentheses
*/
public ArrayList<String> generateParenthesis(int n) {
// Write your code here
ArrayList<String> result = new ArrayList<String>();
if(n<=0)
return result;
int left = 0,right=0;
String str="";
generateParenthesis(n,left,right,str,result);
return result;
}
public void generateParenthesis(int n,int left,int right,String str ,ArrayList<String> result){
if(left == n){
for(int i=0;i< n- right;i++)
str+=")";
result.add(str);
return;
}
generateParenthesis(n, left+1, right,str+"(",result);
if(left>right){
generateParenthesis(n, left, right+1,str+")",result);
}
}
}

lintcode: 生成括号的更多相关文章

  1. [CareerCup] 9.6 Generate Parentheses 生成括号

    9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...

  2. generate parentheses(生成括号)

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

  3. Leetcode 22.生成括号对数

    生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...

  4. [LintCode] Generate Parentheses 生成括号

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

  5. [LeetCode] 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. Generate parentheses,生成括号对,递归,深度优先搜索。

    问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...

  8. 022 Generate Parentheses 生成括号

    给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[  "((()))",  "(()())",  "(())() ...

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

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

随机推荐

  1. win8.1上安装vc6

    win8.1上安装vc6 1.以管理员方式运行SETUP.EXE,然后一路下一步 2.这里需要一点点耐心,等10分钟左右就能过去,电脑会比较卡,有点像假死,还是没有死掉,等等就好了 3.这里选择vc6 ...

  2. 深入浅出Spring(五) SpringMVC

    上一篇深入浅出Spring(四) Spring实例分析的博文中,咱们已经可以了解Spring框架的运行原理和实现过程,接下来咱们继续讲解Spring的一个延伸产品——Spring MVC 1.Spri ...

  3. 20145129 《Java程序设计》第5周学习总结

    20145129 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被打包为对象,可以尝试(try)捕捉(catch)代表 ...

  4. VS2013 IIS Express 添加MIME映射

    VS2013,则可以直接在IIS Express中添加MIME映射.操作如下: 1.在DOS窗口下进入IIS Express安装目录,默认是“C:\Program Files\IIS Express” ...

  5. svn 检出 Check out 请求的名称有效,但是找不到请求的类型的数据。

    根据问题不同有不同的解决方案,可按照以下方法进行解决1.取消TortoiseSVN-网络-代理2.确认SVN目录地址是否正确,可在浏览器中直接打开测试.如地址是由计算机名组成请改成Ip地址进行测试

  6. 《JavaScript高级程序设计》第3章 基本概念

    3.4 数据类型 3.4.1 typeof操作符 var message = 'some string'; console.log(typeof message); // 'string' conso ...

  7. JS学习笔记-1--基本知识和注意事项

    1.JS开始的目的主要是验证表单的输入验证 2.是一种具有面向对象能力的.解释型语言.是基于事件驱动的相对较安全的客户端脚本语言 3.JS 特点:松散型:变量不具备一个明确的类型:   对象属性:把属 ...

  8. C++ Templates之模板元编程

    #ifndef POW3_H #define POW3_H template <int N> class Pow3 { public: enum{result = 3 * Pow3< ...

  9. NYOJ-85 有趣的数 AC 分类: NYOJ 2014-01-17 21:42 240人阅读 评论(0) 收藏

    这道题目就是,找规律,小学奥数,找规律不难吧, #include<stdio.h> int sc(int x); int main(){ int n=0; int num,cs,k; sc ...

  10. Win32 Plus Extra Height of Caption Bar

    you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...