lintcode: 生成括号
生成括号
给定 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: 生成括号的更多相关文章
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- Leetcode 22.生成括号对数
生成括号对数 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n =3,生成结果为: [ "((()))", "( ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] 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 ...
- Generate parentheses,生成括号对,递归,深度优先搜索。
问题描述:给n对括号,生成所有合理的括号对.比如n=2,(()),()() 算法思路:利用深度优先搜索的递归思想,对n进行深度优先搜索.边界条件是n==0:前面电话号组成字符串也是利用dfs. pub ...
- 022 Generate Parentheses 生成括号
给 n 对括号,写一个函数生成所有合适的括号组合.比如,给定 n = 3,一个结果为:[ "((()))", "(()())", "(())() ...
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
随机推荐
- (转)前端构建工具gulp入门教程
前端构建工具gulp入门教程 老婆婆 1.8k 2013年12月30日 发布 推荐 10 推荐 收藏 83 收藏,20k 浏览 本文假设你之前没有用过任何任务脚本(task runner)和命令行工具 ...
- 条款24:若所有的函数参数可能都需要发生类型转换才能使用,请采用non-member函数
假设有一个有理数类Rational,有一个计算有理数乘法的成员函数operator*,示例如下: #include <iostream> class Rational { public: ...
- JS中showModalDialog 详细使用
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...
- 如何把bootstrap用webpack打包
今天下载了一个anguarl2写后台,一直没有找到是如何使用bootstrap样式的,然后就全文做了搜索,发现有一段代码 import 'bootstrap-loader'; 这段代码很可疑,所以就查 ...
- Postgresql 存储过程调试 1
看来人真的有些力不从心,半个月前还很得意掌握的简单的Postgresql 存储过程的调试,一段时间没使用,做新功能就忘了! Postgresql 在开源的数据库里面算是很强悍的了,但现在就是不方便调试 ...
- Js作用域与作用域链详解[转]
一直对Js的作用域有点迷糊,今天偶然读到JavaScript权威指南,立马被吸引住了,写的真不错.我看的是第六版本,相当的厚,大概1000多页,Js博大精深,要熟悉精通需要大毅力大功夫. 一:函数作 ...
- 20145120 《Java程序设计》第7周学习总结
20145120 <Java程序设计>第7周学习总结 教材学习内容总结 Lambda表达式 例:Comparator<String> byLength = (name1, na ...
- 关于四则运算的代码debug测试
1.首先检测题目是否能为负数,0? 截图: 总结:如图所示出题数目为0的时候,并没提示重新输入,而是输出空白,而当输出题目为负数的时候系统提示错误,并且提示终止 2.检测操作值得范围: 总结:当操 ...
- 8、android代码优化技术记录
1.length.length().size的优化 举例: int array_one[] = {1,2,3,4,5,6,7,8,9,10}; int array_two[] = {1,2,3,4,5 ...
- discuz之同步登入
前言 首先感谢dozer学长吧UCenter翻译成C# 博客地址----------->http://www.dozer.cc/ 其次感谢群友快乐々止境同学的热心指导,虽然萍水相逢但让我 ...