22. Generate Parentheses

public class Solution {
    public static List<String> generateParenthesis(int n) {
        List<String> res=new ArrayList<String>();
        func(0,0,"",res,n);
        return res;
        
    }
    public static void func(int left,int right,String temp,List<String> res,int n)
    {
        if(left==n&&right==n)
        {
            res.add(temp);
            return;
        }
        else if(left==right)
        {
            func(left+1,right,temp+"(",res,n);
        }
        else
        {
            if(left+1<=n)
                func(left+1,right,temp+"(",res,n);
            if(right+1<=n)
                func(left,right+1,temp+")",res,n);
        }
    }
    
   
}
原文地址:https://www.cnblogs.com/aguai1992/p/5433969.html