首页 > 其他 > 详细

22. Generate Parentheses

时间:2017-10-17 18:27:30      阅读:352      评论:0      收藏:0      [点我收藏+]

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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

题目含义:给出n对括号组成的所有字符串

 1     private void backTrace(List<String > list,String str,int open,int close,int max)
 2     {
 3         //open代表左括号目前的数量  close代表右括号目前的数量
 4         if (str.length() == max*2)
 5         {
 6             list.add(str);return;
 7         }
 8         if (open<max) backTrace(list,str+"(",open+1,close,max);
 9         if (close<open) backTrace(list,str+")",open,close+1,max);
10     }    
11     
12     public List<String> generateParenthesis(int n) {
13         List<String > list = new ArrayList<>();
14         backTrace(list,"",0,0,n);
15         return list;   
16     }

 

22. Generate Parentheses

原文:http://www.cnblogs.com/wzj4858/p/7682698.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!