Skip to content

Commit 183ab96

Browse files
committed
Time: 27 ms (5.49%), Space: 13.3 MB (75.75%) - LeetHub
1 parent 2bbe988 commit 183ab96

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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+
} else {
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

Comments
 (0)