22. 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:

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

题目含义:给出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     }
原文地址:https://www.cnblogs.com/wzj4858/p/7682698.html