key
解题方案
code
public List<String> generateParenthesis(int n) {
List<String> res = new ArrayList<>();
singleStr(res,"",0,0,n);
return res;
}
//left表示左括号的数量
private void singleStr (List<String> result, String str,int left, int right, int n){
if(left==n&&right==n) //如果都有n个 加入
result.add(str);
if(left<n) //如果左括号少
singleStr(result,str+"(",left+1,right,n);
if(right<left) //如果右括号比左括号少
singleStr(result,str+")",left,right+1,n);
}