-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq22_fast.cpp
32 lines (32 loc) · 921 Bytes
/
q22_fast.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
vector<string> generateParenthesis(int n) {
vector<int> leftcount;
vector<string> r;
if(n==0) return r;
r.push_back("(");
leftcount.push_back(1);
for(int i=1;i<2*n;++i){
int sizer=r.size();
for(int j=0;j<sizer;++j){
if(leftcount[j]>0&&0<2*n-i-leftcount[j]){
r.push_back(r[j]+')');
leftcount.push_back(leftcount[j]-1);
r[j]+='(';
++leftcount[j];
continue;
}
if(leftcount[j]==0){
r[j]+='(';
++leftcount[j];
continue;
}
if(0==2*n-i-leftcount[j]){
r[j]+=')';
--leftcount[j];
}
}
}
return r;
}
};