We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2bbe988 commit 183ab96Copy full SHA for 183ab96
0022-generate-parentheses/0022-generate-parentheses.cpp
@@ -0,0 +1,34 @@
1
+class Solution {
2
+public:
3
+ vector<string> generateParenthesis(int n) {
4
+ n *= 2;
5
+ vector<string> ans;
6
+ for (int i = 1; i < (1 << n); i++) {
7
+ string temp;
8
+ for (int j = 0; j < n; j++) {
9
+ if (i & (1 << j)) {
10
+ temp += '(';
11
+ } else {
12
+ temp += ')';
13
+ }
14
15
+ int stack = 0;
16
+ bool ok = true;
17
+ for (auto& i : temp) {
18
+ if (i == '(') {
19
+ stack++;
20
21
+ if (stack)
22
+ stack--;
23
+ else
24
+ ok = false;
25
26
27
+ ok &= stack == 0;
28
+ if (ok) {
29
+ ans.push_back(temp);
30
31
32
+ return ans;
33
34
+};
0 commit comments