生成括号

给定 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. 在51系列中data,idata,xdata,pdata的区别

    在51系列中data,idata,xdata,pdata的区别: data:固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码 也最小. idata:固定指前面 ...

  2. 学习jax-ws(一)

    1.生成文件时提示class not find ,需要加个cp .,这样就行了 E:\mylearn\learn_webservice\learnJax-ws\bin>wsgen -cp . w ...

  3. PB中获取datawindow提交的sql语句

    PB的群里边,有人问的到这个问题,查了一下,综合了两条回答,得到了答案 1.DW 控件的SQLpreview 事件里的sqlsyntax 参数即是 2.pb一般使用占位符优化SQL语句,也就是你看到的 ...

  4. Objective-C面向对象(一)

    1.类和对象 1.1定义类 面向对象的程序设计中有两个重要概念:类(class)和对象(object),类事某一批对象的抽象,对象是一个具体存在的实体. Objective-C定义类需要氛围2个步骤 ...

  5. Ant学习---第五节:Ant_Junit介绍(基于3的版本)

    Junit3 和 Junit4 有本质上的区别 1.普通java类,代码如下: package learn.junit; public class HelloWorld { public String ...

  6. Liferay IDE 3.1 M1发布啦

    很嗨森,以后就再也不用SDK和下载.ivy啦 新增功能主要有: 1.Liferay Workspace(用来存放Liferay Module项目) 2. Liferay Gradle Module P ...

  7. PKUSC滚粗记

    本来想考得这么烂还是别写了,后来想想毕竟是我高中难得的一次经历,靠脑子记的话我这脑残患者将来肯定会忘了啊……QwQ 好像跟我一样用这个题目的神犇都签了一本QwQ Day 0 来的路上我校其他三位OIe ...

  8. draw call 的优化

    用一张贴图,renderstate都设置成一致,多个draw合并成一个

  9. c++ assert

    #include<iostream> #include <assert.h> using namespace std; int main() { ; assert(a == ) ...

  10. map线程

    来看看map线程到底是如何运行的 很早就知道一个map是一个线程,以后有可能改成一个map一个进程,那就先来看看一个map一个线程是如何运作的 其实刚开始整个服务器就是两个线程,但发现这样服务器支持的 ...