首页 > 其他 > 详细

[LeetCode] 22 - Generate Parentheses

时间:2015-08-29 16:36:47      阅读:222      评论: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 doGenerate(int x0, int x1, string&& s) {
    if (!x0 && !x1) {
      m_result.emplace_back(s);
    }
    if (x0 > 0) {
      doGenerate(x0 -1, x1, s + "(");
    }
    if (x1 > 0 && x1 > x0) {
      doGenerate(x0, x1 - 1 , s + ")");
    }
  }

  vector<string> generateParenthesis(int n) {
    int x0 = n, x1 = n;
    m_result.clear();
    doGenerate(n, n, "");
    return m_result;
  }
private:
  vector<string> m_result;
};

[LeetCode] 22 - Generate Parentheses

原文:http://www.cnblogs.com/shoemaker/p/4769202.html

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