生成括号

给定 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. 调整maven配置文件

    maven的配置文件位置:maven安装位置\conf\settings.xml. 这次调整maven的配置文件主要解决三个问题: 调整本地依赖库位置 设置代理 添加远程资源库镜像节点 调整本地依赖库 ...

  2. 关于Objective-C格式化处理相关规范

    Objective-C格式字符串和C#有很大的差别,下面我们就来看看 在C#中我们可以这么做,简单例举几个: //格式化输出字符串 string word = "world"; s ...

  3. Photoshop/PS中如何写维吾尔语等语言 乱码

    在新疆的朋友都了解很多标语上面都会有汉语.维语等两种语言.有很多维吾尔语.哈萨克语.柯尔克孜语等语言 要在PS 里进行设计处理,这时在Photoshop中进行设计时文字粘贴进来后出现不正常是乱码形式. ...

  4. 【上传AppStore】iOS项目上传到AppStore步骤流程(第一章) - 上传新的app

    1.登录developer.apple.com 2.点击member center后 然后如下图 3.点击certificates Identifiers 进下图 界面基本介绍请看图 : 4. 其次创 ...

  5. Android -- 创建桌面快捷方式

    代码                                                                                    /** * * 返回添加到桌 ...

  6. mui开发webapp(1)

    mui开发注意事项 Mui HTML5开发框架 mui是一个高性能的HTML5开发框架,从UI到效率,都在极力追求原生体验:这个框架自身有一些规则,刚接触的同学不很熟悉,特总结本文:想了解mui更详细 ...

  7. Elasticseach部分语法总结

    索引 在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库 Relational DB -> D ...

  8. The finnacial statements,taxes and cash flow

    This chapter-2 we learn about the the financial statements(财务报表),taxes and cash flow.We must pay par ...

  9. Android中的单位及测试相关概念

    android中的单位: in 英寸 pt 点距 px 像素  dp(dip) 密度无关的像素单位,自适应device屏幕的比例,通常涉及长宽高时采用 sp 与范围无关的像素单位,通常在设置字体大小时 ...

  10. SQL Server性能优化(2)获取基本信息

    以下常用的SQL语句有利于我们分析数据库的基本信息,然后根据查询的结果进行优化. 1. 查看索引碎片     无论何时对基础数据执行插入.更新或删除操作,SQL Server 数据库引擎都会自动维护索 ...