码迷,mamicode.com
首页 > 其他好文 > 详细

22. Generate Parentheses

时间:2015-12-19 23:06:33      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

22. Generate Parentheses

Total Accepted: 70039 Total Submissions: 203043 Difficulty: Medium

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:

"((()))", "(()())", "(())()", "()(())", "()()()"

Hide Tags

 
class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> res;
        generateParenthesis(res,"",n,n);
        return res;
    }
    void generateParenthesis(vector<string>& res,string str,int left,int right){
        if(left ==0 && right == 0 ){
            res.push_back(str);
            return ;
        }
        if(left>0) generateParenthesis(res,str+"(",left-1,right);
        if(right>left) generateParenthesis(res,str+")",left,right-1);
    }
};

22. Generate Parentheses

标签:

原文地址:http://www.cnblogs.com/zengzy/p/5059845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!