# 第十九周ARTS总结
# Algorithm
2ms | 28.71% Run time 39.2MB | 57.01% Memory
public List<String> generateParenthesis(int n) {
List<Store> ans = new ArrayList<>();
List<Store> temp = new ArrayList<>();
int count = n * 2;
Store first = new Store();
first.str = "(";
first.leftCount = 1;
first.rightCount = 0;
ans.add(first);
for (int i = 1; i < count; i++) {
temp.clear();
for (int j = 0; j < ans.size(); j++) {
Store cur = ans.get(j);
// 加左括号
if (cur.leftCount < n) {
Store left = new Store();
left.str = cur.str + "(";
left.leftCount = cur.leftCount + 1;
left.rightCount = cur.rightCount;
temp.add(left);
}
// 加右括号
if (cur.leftCount > cur.rightCount) {
Store right = new Store();
right.str = cur.str + ")";
right.leftCount = cur.leftCount;
right.rightCount = cur.rightCount + 1;
temp.add(right);
}
}
ans.clear();
ans.addAll(temp);
}
List<String> realAns = new ArrayList<>();
for (Store store : ans) {
realAns.add(store.str);
}
return realAns;
}
public static class Store {
private String str;
private int leftCount;
private int rightCount;
}
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Review
# Tip
- 广播接收器分为静态注册和动态注册,静态注册的接收器能够在APP不启动的情况下接收到广播
# Share
暂无内容
← 第十八周ARTS总结 第二十周ARTS总结 →