Skip to content

Commit e49b738

Browse files
committed
feat: add python and java solutions to lcci question: 08.09.Bracket
添加《程序员面试金典》题解:面试题 08.09. 括号
1 parent 34ac757 commit e49b738

File tree

4 files changed

+180
-41
lines changed

4 files changed

+180
-41
lines changed

lcci/08.09.Bracket/README.md

+47-1
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,62 @@
2525

2626
### Python3
2727
<!-- 这里可写当前语言的特殊实现逻辑 -->
28+
递归求解。其中,`left` 表示剩余的 `(``right` 表示剩余的 `)`
29+
30+
-`left` > `right` 时,说明 state 中 `(` 少于 `)`,不是合法组合,直接剪枝;
31+
-`right` == 0 时,说明 state 组合完毕;
32+
-`left` > 0 时,此时可往 state 添加一个 `(`
33+
-`right` > 0 时,此时可往 state 添加一个 `)`
2834

29-
```python
3035

36+
```python
37+
class Solution:
38+
def generateParenthesis(self, n: int) -> List[str]:
39+
res = []
40+
def generate(state, left, right):
41+
# 剩余的`(`多于`)`
42+
if left > right:
43+
return
44+
if right == 0:
45+
res.append(state)
46+
return
47+
if left > 0:
48+
generate(state + '(', left - 1, right)
49+
if right > 0:
50+
generate(state + ')', left, right - 1)
51+
generate('', n, n)
52+
return res
3153
```
3254

3355
### Java
3456
<!-- 这里可写当前语言的特殊实现逻辑 -->
3557

3658
```java
59+
class Solution {
60+
List<String> res;
61+
62+
public List<String> generateParenthesis(int n) {
63+
res = new ArrayList<>();
64+
generate("", n, n);
65+
return res;
66+
}
3767

68+
private void generate(String state, int left, int right) {
69+
if (left > right) {
70+
return;
71+
}
72+
if (right == 0) {
73+
res.add(state);
74+
return;
75+
}
76+
if (left > 0) {
77+
generate(state + "(", left - 1, right);
78+
}
79+
if (right > 0) {
80+
generate(state + ")", left, right - 1);
81+
}
82+
}
83+
}
3884
```
3985

4086
### ...

lcci/08.09.Bracket/README_EN.md

+93-40
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,93 @@
1-
# [08.09. Bracket](https://leetcode-cn.com/problems/bracket-lcci)
2-
3-
## Description
4-
<p>Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n pairs of parentheses.</p>
5-
6-
<p>Note: The result set should not contain duplicated subsets.</p>
7-
8-
<p>For example, given&nbsp;n = 3, the result should be:</p>
9-
10-
<pre>
11-
[
12-
&quot;((()))&quot;,
13-
&quot;(()())&quot;,
14-
&quot;(())()&quot;,
15-
&quot;()(())&quot;,
16-
&quot;()()()&quot;
17-
]
18-
</pre>
19-
20-
21-
22-
## Solutions
23-
24-
25-
### Python3
26-
27-
```python
28-
29-
```
30-
31-
### Java
32-
33-
```java
34-
35-
```
36-
37-
### ...
38-
```
39-
40-
```
1+
# [08.09. Bracket](https://leetcode-cn.com/problems/bracket-lcci)
2+
3+
## Description
4+
<p>Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n pairs of parentheses.</p>
5+
6+
7+
8+
<p>Note: The result set should not contain duplicated subsets.</p>
9+
10+
11+
12+
<p>For example, given&nbsp;n = 3, the result should be:</p>
13+
14+
15+
16+
<pre>
17+
18+
[
19+
20+
&quot;((()))&quot;,
21+
22+
&quot;(()())&quot;,
23+
24+
&quot;(())()&quot;,
25+
26+
&quot;()(())&quot;,
27+
28+
&quot;()()()&quot;
29+
30+
]
31+
32+
</pre>
33+
34+
35+
36+
37+
## Solutions
38+
39+
40+
### Python3
41+
42+
```python
43+
class Solution:
44+
def generateParenthesis(self, n: int) -> List[str]:
45+
res = []
46+
def generate(state, left, right):
47+
if left > right:
48+
return
49+
if right == 0:
50+
res.append(state)
51+
return
52+
if left > 0:
53+
generate(state + '(', left - 1, right)
54+
if right > 0:
55+
generate(state + ')', left, right - 1)
56+
generate('', n, n)
57+
return res
58+
```
59+
60+
### Java
61+
62+
```java
63+
class Solution {
64+
List<String> res;
65+
66+
public List<String> generateParenthesis(int n) {
67+
res = new ArrayList<>();
68+
generate("", n, n);
69+
return res;
70+
}
71+
72+
private void generate(String state, int left, int right) {
73+
if (left > right) {
74+
return;
75+
}
76+
if (right == 0) {
77+
res.add(state);
78+
return;
79+
}
80+
if (left > 0) {
81+
generate(state + "(", left - 1, right);
82+
}
83+
if (right > 0) {
84+
generate(state + ")", left, right - 1);
85+
}
86+
}
87+
}
88+
```
89+
90+
### ...
91+
```
92+
93+
```

lcci/08.09.Bracket/Solution.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
List<String> res;
3+
4+
public List<String> generateParenthesis(int n) {
5+
res = new ArrayList<>();
6+
generate("", n, n);
7+
return res;
8+
}
9+
10+
private void generate(String state, int left, int right) {
11+
if (left > right) {
12+
return;
13+
}
14+
if (right == 0) {
15+
res.add(state);
16+
return;
17+
}
18+
if (left > 0) {
19+
generate(state + "(", left - 1, right);
20+
}
21+
if (right > 0) {
22+
generate(state + ")", left, right - 1);
23+
}
24+
}
25+
}

lcci/08.09.Bracket/Solution.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def generateParenthesis(self, n: int) -> List[str]:
3+
res = []
4+
def generate(state, left, right):
5+
if left > right:
6+
return
7+
if right == 0:
8+
res.append(state)
9+
return
10+
if left > 0:
11+
generate(state + '(', left - 1, right)
12+
if right > 0:
13+
generate(state + ')', left, right - 1)
14+
generate('', n, n)
15+
return res

0 commit comments

Comments
 (0)