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相关推荐

  1. LeetCode Generate Parentheses

    原题链接在这里:https://leetcode.com/problems/generate-parentheses/ 题目: Given n pairs of parentheses, write ...

  2. [LeetCode] Generate Parentheses

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

  3. [leetcode]Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  4. LeetCode:Generate Parentheses

    题目链接 Given n pairs of parentheses, write a function to generate all combinations of well-formed pare ...

  5. [leetcode] 22. Generate Parentheses

    题目大意 https://leetcode.com/problems/generate-parentheses/description/ 22. Generate Parentheses Given ...

  6. LeetCode算法入门- Generate Parentheses -day16

    LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to gen ...

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

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

  8. 【LeetCode每天一题】Generate Parentheses(创造有效的括弧)

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

  9. 【LeetCode从零单排】No22.Generate Parentheses

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

  10. [LeetCode] #22 Generate Parentheses

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

最新文章

  1. 硕导跳槽高中当老师,博士扎堆中小学,是内卷还是进步?
  2. Manacher 算法模板
  3. 28-高级特性之作用域(2)
  4. Java File类void deleteOnExit()方法(带示例)
  5. 5G精华问答:5G的速度到底有多快?| 技术头条
  6. DSDV的问题(Problems of DSDV)
  7. Leecode-2 Add Two Numbers
  8. 如何在Swift中掌握协议
  9. 2021年百度账号批量取消绑定手机号教程
  10. LSI Logic 1068 SAS 磁盘阵列卡配置教程
  11. css中子元素设置margin-top会影响到父元素
  12. android 7 uc flash player,大神教你解决win7系统uc浏览器提示flash player遭到阻止的详细办法...
  13. (不重点考)试算平衡的分类
  14. scope曲线用plot画出、多个scope用plot画出、scop数据保存至工作空间
  15. echart自定义动画_ECharts使用—折线图动态加载
  16. MATLAB暗原色先验去雾算法的一些认识
  17. Hadoop学习(1)--Hadoop介绍
  18. 中国历史“四大谜案”
  19. 华罗庚统筹法与计算机专业,华罗庚的《统筹方法》
  20. 上海大学计算机科学与技术专业选修课怎么选,大一“选修课”怎么选?这3种选课攻略很关键,第3种才是最重要的!...

热门文章

  1. 用栈实现队列,实现Enqueue和Dequeue方法
  2. 查询Mysql的数据架构信息研究
  3. ThinkPHP5.1设置404页面
  4. Django配置文件常用信息
  5. 关于WEB ServiceWCFWebApi实现身份验证之WEB Service篇
  6. Java期末设计(十三周)
  7. 动态添加table表格内容,填充审批意见。
  8. 完美解决Win8声卡爆音破音、卡顿
  9. ZOJ 3077 Move to Baggage Office
  10. 向中级程序员转变的10个建议