1. 程式人生 > >【leetcode】22. Generate Parentheses

【leetcode】22. Generate Parentheses

提交程式碼(遞迴):

class Solution {
   List<String> generateParenthesis(int n) {
    	List<String> list=new ArrayList<>();
		append(n,n,"",list);
		return list;
}
  void append(int left,int right,String cur,List<String> list) {
    	if(left<0||right<0||left>right)	return;
    	if
(left==0&&right==0) { list.add(cur); return; } append(left-1,right,cur+"(",list); append(left,right-1,cur+")",list); } }