程式語言 - LeetCode - C++ - 22. Generate Parentheses



參考資訊:
https://algo.monster/liteproblems/22
https://www.cnblogs.com/grandyang/p/4444160.html

題目:


解答:

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> r;

        auto dfs = [&](this auto&& dfs, int left, int right, string s) -> int {
            if ((left > n) || (right > n) || (left < right)) {
                return 0;
            }

            if ((left == n) && (right == n)) {
                r.push_back(s);
                return 1;
            }

            dfs(left + 1, right, s + "(");
            dfs(left, right + 1, s + ")");
            return 0;
        };

        dfs(0, 0, "");
        return r;
    }
};