码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Generate Parentheses

时间:2014-07-19 15:28:21      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   for   re   

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+应该可以改进

LeetCode Generate Parentheses,布布扣,bubuko.com

LeetCode Generate Parentheses

标签:style   blog   color   io   for   re   

原文地址:http://www.cnblogs.com/lailailai/p/3854132.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!