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

leetcode[22]Generate Parentheses

时间:2015-02-10 14:48:16      阅读:164      评论: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:

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

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

 

leetcode[22]Generate Parentheses

标签:

原文地址:http://www.cnblogs.com/Vae98Scilence/p/4283661.html

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