首页 > 代码库 > leetcode 22 Generate Parentheses

leetcode 22 Generate Parentheses

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        set<string> t;
        if (n == 0) t.insert("");
        else {
            vector<string> pre = generateParenthesis(n - 1);
            for (auto a : pre) {
                for (int i = 0; i < a.size(); ++i) {
                    if (a[i] == ‘(‘) {
                        a.insert(a.begin() + i + 1, ‘(‘);
                        a.insert(a.begin() + i + 2, ‘)‘);
                        t.insert(a);
                        a.erase(a.begin() + i + 1, a.begin() + i + 3);
                    }
                }
                t.insert("()" + a);
            }
        }
        return vector<string>(t.begin(), t.end());
    }
};

leetcode 22 Generate Parentheses