原题地址:https://leetcode.com/problems/generate-parentheses/

解决方法:回溯法

class Solution {
private:
    vector<string> coll;
    void helper(string s, int left, int right){
        if(left > right || left < 0 || right < 0)
            return;
        if(0 == left && 0 == right){
            coll.push_back(s);
            return;
        }
        string lString = s, rString = s;
        helper(lString += '(', left - 1, right);
        helper(rString += ')', left, right - 1);
    }
public:
    vector<string> generateParenthesis(int n) {
        string s;
        helper(s, n, n);
        return coll;
    }
};

 

相关文章:

  • 2021-10-19
  • 2022-02-15
  • 2022-01-15
  • 2021-11-04
  • 2021-08-12
猜你喜欢
  • 2021-07-14
  • 2021-05-23
  • 2021-05-06
  • 2021-09-30
  • 2021-06-30
  • 2022-01-03
  • 2022-01-12
相关资源
相似解决方案