solution.md 3.3 KB
Newer Older
1
# 括号生成
F
fix bug  
feilong 已提交
2

3
<p>数字 <code>n</code> 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且 <strong>有效的 </strong>括号组合。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>n = 3<strong><br />输出:</strong>["((()))","(()())","(())()","()(())","()()()"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>n = 1<strong><br />输出:</strong>["()"]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>1 <= n <= 8</code></li></ul>
每日一练社区's avatar
fix bug  
每日一练社区 已提交
4
<p>以下错误的选项是?</p>
F
fix bug  
feilong 已提交
5

6
## aop
F
fix bug  
feilong 已提交
7

8
### before
F
fix bug  
feilong 已提交
9

10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
```cpp
int main()
{
    Solution sol;
    int n = 3;
    vector<string> res;
    res = sol.generateParenthesis(n);
    for (auto i : res)
        cout << i << endl;

    return 0;
}

```

## 答案
F
fix bug  
feilong 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
```cpp
class Solution
{
public:
    vector<string> generateParenthesis(int n)
    {

        vector<string> ans;
        string s;
        back(0, 2 * n, 0, 0, s, ans);
        return ans;
    }
    void back(int left, int right, int l_n, int r_n, string &s, vector<string> &ans)
    {
        if (r_n > l_n || l_n > right)
            return;
        if (left == right)
        {
            ans.push_back(s);
            return;
        }
        s += '(';
        l_n++;
        left++;
        back(left, right, l_n, r_n, s, ans);
        s.erase(s.end() - 1);
        l_n--;
        s += ')';
        r_n++;
        back(left, right, l_n, r_n, s, ans);
        s.erase(s.end() - 1);
        r_n--;
    }
};

```
## 选项

F
fix bug  
feilong 已提交
71

72
### A
F
fix bug  
feilong 已提交
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
```cpp
class Solution
{
public:
    void generateParenthesis(string i, int left, int right, vector<string> &output)
    {
        if (left == 0 && right == 0)
        {
            output.push_back(i);
            return;
        }
        if (left > 0)
        {
            generateParenthesis(i + '(', left - 1, right, output);
        }
        if (left < right)
        {
            generateParenthesis(i + ')', left, right - 1, output);
        }
    }

    vector<string> generateParenthesis(int n)
    {
        string s = "";
        vector<string> output;
        generateParenthesis(s, n, n, output);
        return output;
    }
};
```

### B
F
fix bug  
feilong 已提交
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
```cpp
class Solution
{
public:
    vector<string> res;
    void backtrack(int n, int key, string cur)
    {
        if (key < 0 || key > n)
            return;
        if (cur.size() == 2 * n)
        {
            if (key == 0)
                res.push_back(cur);
            return;
        }
        backtrack(n, key - 1, cur + ')');
        backtrack(n, key + 1, cur + '(');
    }
    vector<string> generateParenthesis(int n)
    {
        if (n == 0)
            return vector<string>{""};
        string cur("(");
        backtrack(n, 1, cur);
        return res;
    }
};

```

### C
F
fix bug  
feilong 已提交
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
```cpp
class Solution
{
public:
    vector<string> generateParenthesis(int n)
    {
        vector<string> res;
        dfs("", 0, n, &res);
        return res;
    }

    void dfs(const string &path, int m, int n, vector<string> *res)
    {

        if (m == 0 && n == 0)
        {
            if (!path.empty())
                res->push_back(path);
            return;
        }

        if (n > 0)
            dfs(path + '(', m + 1, n - 1, res);
        if (m > 0)
            dfs(path + ')', m - 1, n, res);
    }
};
```