leetcode 22. Generate Parentheses 巧妙的递归形式

leetcode 22. Generate Parentheses



public class Solution {
    
    
    public List generateParenthesis(int n) {
        List res = new ArrayList();
        int open =0;
        int close=0;
        genThesis(res,open,close,n,"");
        return res;
    }
    
    
    public void genThesis(List res,int open,int close,int n,String s){
        if(open+close==2*n){    //return the length
            res.add(s);
        }
        if(openclose){
            genThesis(res,open,close+1,n,s+")"); // add ) only when open >close
        }
    }
}


你可能感兴趣的:(算法)