腾讯///括号生成

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合。

例如,给出 = 3,生成结果为:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

 

C++:

1、深搜

class Solution {
public:
    void dfs(int level, int l, int r, vector<string> &solution, string s, int n){
        if(2*n == level) solution.push_back(s);
        if(l <= n-1)
            dfs(level+1, l+1, r, solution, s+'(', n);
        if(l >= r+1&&r <= n-1)
            dfs(level+1, l, r+1, solution, s+')', n);
    }
    vector<string> generateParenthesis(int n) {
        vector<string> solution;
        dfs(0, 0, 0, solution, "", n);
        return solution;
    }
};

2、迭代

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        vector<string> re;
        if(n==0) return re;
        re.push_back("");
        for(int i = 0; i < 2*n; i++){
            vector<string> temp;
            for(int i = 0; i < re.size(); i++){
                int l = 0, r = 0;
                for(char x:re[i]){
                    if(x == '(')
                        l++;
                    if(x == ')')
                        r++;
                }
                if(l < n)
                    temp.push_back(re[i]+'(');
                if(r+1<=l)
                    temp.push_back(re[i]+')');
            }
            re = temp;
        }
        return re;
    }
};

3、宽搜

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        queue<string>re;
        re.push("");
        int  level=0;
        while (level++!=2*n){
            int length=re.size();
            for (int i=0; i<length; ++i){
                string temp=re.front();
                re.pop();
                int l = 0, r = 0;
                for (auto y : temp){
                    if (y == '(') ++l;
                    if (y == ')') ++r;
                }
                if (l<n) re.push(temp + '(');
                if (r<l) re.push(temp + ')');
            }
        }
        vector<string>ans;
        while(!re.empty()){
            ans.push_back(re.front());
            re.pop();
        }
        return ans;
    }
};

 

posted @ 2018-10-29 12:35  strawqqhat  阅读(191)  评论(0编辑  收藏  举报
#home h1{ font-size:45px; } body{ background-image: url("放你的背景图链接"); background-position: initial; background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-origin: initial; background-clip: initial; height:100%; width:100%; } #home{ opacity:0.7; } .wall{ position: fixed; top: 0; left: 0; bottom: 0; right: 0; } div#midground{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -1; -webkit-animation: cc 200s linear infinite; -moz-animation: cc 200s linear infinite; -o-animation: cc 200s linear infinite; animation: cc 200s linear infinite; } div#foreground{ background: url("https://i.postimg.cc/z3jZZD1B/foreground.png"); z-index: -2; -webkit-animation: cc 253s linear infinite; -o-animation: cc 253s linear infinite; -moz-animation: cc 253s linear infinite; animation: cc 253s linear infinite; } div#top{ background: url("https://i.postimg.cc/PP5GtGtM/midground.png"); z-index: -4; -webkit-animation: da 200s linear infinite; -o-animation: da 200s linear infinite; animation: da 200s linear infinite; } @-webkit-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-o-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @-moz-keyframes cc { from{ background-position: 0 0; transform: translateY(10px); } to{ background-position: 600% 0; } } @keyframes cc { 0%{ background-position: 0 0; } 100%{ background-position: 600% 0; } } @keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-webkit-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-moz-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } } @-ms-keyframes da { 0%{ background-position: 0 0; } 100%{ background-position: 0 600%; } }