首页 > 其他 > 详细

Generate Parentheses

时间:2015-03-20 23:19:24      阅读:291      评论: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: 

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

  这题没做出来,惭愧。

solution:

void dfs(vector<string> &result,string str,int left,int right)
{
    if(left > right)
        return;
    if (left == 0 && right == 0)
    {
        result.push_back(str);
        return;
    }
    if(left > 0)
        dfs(result, str+"(", left-1, right);
    if(right > 0)
        dfs(result, str+")", left, right-1);
}

vector<string> generateParenthesis(int n) {
    vector<string> res;
    if(n < 1) 
        return res;
    dfs(res, "", n, n);
    return res;
}

原文链接:http://blog.csdn.net/linhuanmars/article/details/19873463

Generate Parentheses

原文:http://www.cnblogs.com/gattaca/p/4354700.html

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