首页 > 其他 > 详细

LeetCode---Generate Parentheses

时间:2014-11-13 16:35:26      阅读:215      评论:0      收藏:0      [点我收藏+]

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

原文:http://blog.csdn.net/ivyvae/article/details/41081497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!