首页 > 代码库 > LeetCode---Generate Parentheses

LeetCode---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:

"((()))", "(()())", "(())()", "()(())", "()()()"
解决方案:
public class Solution {
    public void unguarded_generate(List<String> result,String curr,int m,int n){
        if(m==0&&n==0){
            result.add(curr);
        }
        else
        {
            if(m!=0){
                unguarded_generate(result,curr+"(",m-1,n);
            }
            if(m<n&&n!=0){
                unguarded_generate(result,curr+")",m,n-1);
            }
        }
        
    }
    
    
    public List<String> generateParenthesis(int n) {
        List<String> result=new ArrayList<String>();
        if(n>0){
            unguarded_generate(result,new String(),n,n);
        }
        return result;
    }
}



LeetCode---Generate Parentheses