首页 > 代码库 > LeetCode Generate Parentheses

LeetCode Generate Parentheses

class Solution {public:    vector<string> generateParenthesis(int n) {        string str;        vector<string> res;        dfs(n, 0, 0, str, res);        return res;    }    void dfs(int n, int L, int R, string str, vector<string> &res) {        if (str.size() == n<<1) {            res.push_back(str);            return;        }        int len = str.length();        for (int i = L + 1; i <= n; i++) {            dfs(n, i, R, str + "(", res);        }        for (int i = R + 1; i <= n && R < L; i++) {            dfs(n, L, i, str +")", res);        }    }};

dfs暴力搜索,800ms+应该可以改进