Leetcode: Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"
又是把所有满足条件的结果存到一个ArrayList里面, 之前也有类似的如Letter combination of a Phone Number这道题。这种类型的题其实形成了一个套路,套路就是,recursion参数包括最终结果的集合(ArrayList),input(String),递归层次level(int),某一条具体的路径Path
Code Ganker的做法,高手的代码就是简洁啊
1 public ArrayList<String> generateParenthesis(int n) { 2 ArrayList<String> res = new ArrayList<String>(); 3 if(n<=0) 4 return res; 5 helper(n,n,new String(),res); 6 return res; 7 } 8 private void helper(int l, int r, String item, ArrayList<String> res) 9 { 10 if(r<l) 11 return; 12 if(l==0 && r==0) 13 { 14 res.add(item); 15 } 16 if(l>0) 17 helper(l-1,r,item+"(",res); 18 if(r>0) 19 helper(l,r-1,item+")",res); 20 }
Leetcode: Generate Parentheses相关推荐
- LeetCode Generate Parentheses
原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...
- [LeetCode] 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 paren ...
- LeetCode:Generate Parentheses
题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...
- [leetcode] 22. Generate Parentheses
题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...
- LeetCode算法入门- Generate Parentheses -day16
LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 【LeetCode从零单排】No22.Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- [LeetCode] #22 Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
最新文章
- 硕导跳槽高中当老师,博士扎堆中小学,是内卷还是进步?
- Manacher 算法模板
- 28-高级特性之作用域(2)
- Java File类void deleteOnExit()方法(带示例)
- 5G精华问答:5G的速度到底有多快?| 技术头条
- DSDV的问题(Problems of DSDV)
- Leecode-2 Add Two Numbers
- 如何在Swift中掌握协议
- 2021年百度账号批量取消绑定手机号教程
- LSI Logic 1068 SAS 磁盘阵列卡配置教程
- css中子元素设置margin-top会影响到父元素
- android 7 uc flash player,大神教你解决win7系统uc浏览器提示flash player遭到阻止的详细办法...
- (不重点考)试算平衡的分类
- scope曲线用plot画出、多个scope用plot画出、scop数据保存至工作空间
- echart自定义动画_ECharts使用—折线图动态加载
- MATLAB暗原色先验去雾算法的一些认识
- Hadoop学习(1)--Hadoop介绍
- 中国历史“四大谜案”
- 华罗庚统筹法与计算机专业,华罗庚的《统筹方法》
- 上海大学计算机科学与技术专业选修课怎么选,大一“选修课”怎么选?这3种选课攻略很关键,第3种才是最重要的!...