Skip to content

Commit f0d1c11

Browse files
committed
feat: add solutions to lc/lcci/lcof2 problem: Generate Parentheses
1 parent aec0298 commit f0d1c11

File tree

11 files changed

+360
-88
lines changed

11 files changed

+360
-88
lines changed

lcci/08.09.Bracket/README.md

+91-35
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@
2323

2424
## 解法
2525

26-
<!-- 这里可写通用的实现逻辑 -->
27-
28-
递归求解。其中,`left` 表示剩余的 `(``right` 表示剩余的 `)`
29-
30-
-`left` > `right` 时,说明 state 中 `(` 少于 `)`,不是合法组合,直接剪枝;
31-
-`right` == 0 时,说明 state 组合完毕;
32-
-`left` > 0 时,此时可往 state 添加一个 `(`
33-
-`right` > 0 时,此时可往 state 添加一个 `)`
26+
深度优先搜索 DFS。
3427

3528
<!-- tabs:start -->
3629

@@ -41,20 +34,19 @@
4134
```python
4235
class Solution:
4336
def generateParenthesis(self, n: int) -> List[str]:
44-
res = []
45-
def generate(state, left, right):
46-
# 剩余的`(`多于`)`
47-
if left > right:
48-
return
49-
if right == 0:
50-
res.append(state)
37+
ans = []
38+
39+
def dfs(left, right, t):
40+
if left == n and right == n:
41+
ans.append(t)
5142
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
43+
if left < n:
44+
dfs(left + 1, right, t + '(')
45+
if right < left:
46+
dfs(left, right + 1, t + ')')
47+
48+
dfs(0, 0, '')
49+
return ans
5850
```
5951

6052
### **Java**
@@ -63,29 +55,93 @@ class Solution:
6355

6456
```java
6557
class Solution {
66-
List<String> res;
67-
6858
public List<String> generateParenthesis(int n) {
69-
res = new ArrayList<>();
70-
generate("", n, n);
71-
return res;
59+
List<String> ans = new ArrayList<>();
60+
dfs(0, 0, n, "", ans);
61+
return ans;
7262
}
7363

74-
private void generate(String state, int left, int right) {
75-
if (left > right) {
64+
private void dfs(int left, int right, int n, String t, List<String> ans) {
65+
if (left == n && right == n) {
66+
ans.add(t);
7667
return;
7768
}
78-
if (right == 0) {
79-
res.add(state);
80-
return;
69+
if (left < n) {
70+
dfs(left + 1, right, n, t + "(", ans);
8171
}
82-
if (left > 0) {
83-
generate(state + "(", left - 1, right);
72+
if (right < left) {
73+
dfs(left, right + 1, n, t + ")", ans);
8474
}
85-
if (right > 0) {
86-
generate(state + ")", left, right - 1);
75+
}
76+
}
77+
```
78+
79+
### **TypeScript**
80+
81+
```ts
82+
function generateParenthesis(n: number): string[] {
83+
let ans = [];
84+
dfs(0, 0, n, '', ans);
85+
return ans;
86+
};
87+
88+
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
89+
if (left == n && right == n) {
90+
ans.push(t);
91+
return;
92+
}
93+
if (left < n) {
94+
dfs(left + 1, right, n, t + '(', ans);
95+
}
96+
if (right < left) {
97+
dfs(left, right + 1, n, t + ')', ans);
98+
}
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
vector<string> generateParenthesis(int n) {
108+
vector<string> ans;
109+
dfs(0, 0, n, "", ans);
110+
return ans;
111+
}
112+
113+
void dfs(int left, int right, int n, string t, vector<string>& ans) {
114+
if (left == n && right == n)
115+
{
116+
ans.push_back(t);
117+
return;
87118
}
119+
if (left < n) dfs(left + 1, right, n, t + "(", ans);
120+
if (right < left) dfs(left, right + 1, n, t + ")", ans);
88121
}
122+
};
123+
```
124+
125+
### **Go**
126+
127+
```go
128+
func generateParenthesis(n int) []string {
129+
var ans []string
130+
dfs(0, 0, n, "", &ans)
131+
return ans
132+
}
133+
134+
func dfs(left, right, n int, t string, ans *[]string) {
135+
if left == n && right == n {
136+
*ans = append(*ans, t)
137+
return
138+
}
139+
if left < n {
140+
dfs(left+1, right, n, t+"(", ans)
141+
}
142+
if right < left {
143+
dfs(left, right+1, n, t+")", ans)
144+
}
89145
}
90146
```
91147

lcci/08.09.Bracket/README_EN.md

+90-26
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,112 @@
3737
```python
3838
class Solution:
3939
def generateParenthesis(self, n: int) -> List[str]:
40-
res = []
41-
def generate(state, left, right):
42-
if left > right:
43-
return
44-
if right == 0:
45-
res.append(state)
40+
ans = []
41+
42+
def dfs(left, right, t):
43+
if left == n and right == n:
44+
ans.append(t)
4645
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
46+
if left < n:
47+
dfs(left + 1, right, t + '(')
48+
if right < left:
49+
dfs(left, right + 1, t + ')')
50+
51+
dfs(0, 0, '')
52+
return ans
5353
```
5454

5555
### **Java**
5656

5757
```java
5858
class Solution {
59-
List<String> res;
60-
6159
public List<String> generateParenthesis(int n) {
62-
res = new ArrayList<>();
63-
generate("", n, n);
64-
return res;
60+
List<String> ans = new ArrayList<>();
61+
dfs(0, 0, n, "", ans);
62+
return ans;
6563
}
6664

67-
private void generate(String state, int left, int right) {
68-
if (left > right) {
65+
private void dfs(int left, int right, int n, String t, List<String> ans) {
66+
if (left == n && right == n) {
67+
ans.add(t);
6968
return;
7069
}
71-
if (right == 0) {
72-
res.add(state);
73-
return;
70+
if (left < n) {
71+
dfs(left + 1, right, n, t + "(", ans);
7472
}
75-
if (left > 0) {
76-
generate(state + "(", left - 1, right);
73+
if (right < left) {
74+
dfs(left, right + 1, n, t + ")", ans);
7775
}
78-
if (right > 0) {
79-
generate(state + ")", left, right - 1);
76+
}
77+
}
78+
```
79+
80+
### **TypeScript**
81+
82+
```ts
83+
function generateParenthesis(n: number): string[] {
84+
let ans = [];
85+
dfs(0, 0, n, '', ans);
86+
return ans;
87+
};
88+
89+
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
90+
if (left == n && right == n) {
91+
ans.push(t);
92+
return;
93+
}
94+
if (left < n) {
95+
dfs(left + 1, right, n, t + '(', ans);
96+
}
97+
if (right < left) {
98+
dfs(left, right + 1, n, t + ')', ans);
99+
}
100+
}
101+
```
102+
103+
### **C++**
104+
105+
```cpp
106+
class Solution {
107+
public:
108+
vector<string> generateParenthesis(int n) {
109+
vector<string> ans;
110+
dfs(0, 0, n, "", ans);
111+
return ans;
112+
}
113+
114+
void dfs(int left, int right, int n, string t, vector<string>& ans) {
115+
if (left == n && right == n)
116+
{
117+
ans.push_back(t);
118+
return;
80119
}
120+
if (left < n) dfs(left + 1, right, n, t + "(", ans);
121+
if (right < left) dfs(left, right + 1, n, t + ")", ans);
81122
}
123+
};
124+
```
125+
126+
### **Go**
127+
128+
```go
129+
func generateParenthesis(n int) []string {
130+
var ans []string
131+
dfs(0, 0, n, "", &ans)
132+
return ans
133+
}
134+
135+
func dfs(left, right, n int, t string, ans *[]string) {
136+
if left == n && right == n {
137+
*ans = append(*ans, t)
138+
return
139+
}
140+
if left < n {
141+
dfs(left+1, right, n, t+"(", ans)
142+
}
143+
if right < left {
144+
dfs(left, right+1, n, t+")", ans)
145+
}
82146
}
83147
```
84148

lcci/08.09.Bracket/Solution.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
vector<string> generateParenthesis(int n) {
4+
vector<string> ans;
5+
dfs(0, 0, n, "", ans);
6+
return ans;
7+
}
8+
9+
void dfs(int left, int right, int n, string t, vector<string>& ans) {
10+
if (left == n && right == n)
11+
{
12+
ans.push_back(t);
13+
return;
14+
}
15+
if (left < n) dfs(left + 1, right, n, t + "(", ans);
16+
if (right < left) dfs(left, right + 1, n, t + ")", ans);
17+
}
18+
};

lcci/08.09.Bracket/Solution.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func generateParenthesis(n int) []string {
2+
var ans []string
3+
dfs(0, 0, n, "", &ans)
4+
return ans
5+
}
6+
7+
func dfs(left, right, n int, t string, ans *[]string) {
8+
if left == n && right == n {
9+
*ans = append(*ans, t)
10+
return
11+
}
12+
if left < n {
13+
dfs(left+1, right, n, t+"(", ans)
14+
}
15+
if right < left {
16+
dfs(left, right+1, n, t+")", ans)
17+
}
18+
}

lcci/08.09.Bracket/Solution.java

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
class Solution {
2-
List<String> res;
3-
42
public List<String> generateParenthesis(int n) {
5-
res = new ArrayList<>();
6-
generate("", n, n);
7-
return res;
3+
List<String> ans = new ArrayList<>();
4+
dfs(0, 0, n, "", ans);
5+
return ans;
86
}
97

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);
8+
private void dfs(int left, int right, int n, String t, List<String> ans) {
9+
if (left == n && right == n) {
10+
ans.add(t);
1611
return;
1712
}
18-
if (left > 0) {
19-
generate(state + "(", left - 1, right);
13+
if (left < n) {
14+
dfs(left + 1, right, n, t + "(", ans);
2015
}
21-
if (right > 0) {
22-
generate(state + ")", left, right - 1);
16+
if (right < left) {
17+
dfs(left, right + 1, n, t + ")", ans);
2318
}
2419
}
2520
}

0 commit comments

Comments
 (0)