Skip to content

Commit 3b0951e

Browse files
committed
feat: add solutions to lcof2 problem: No.085. Generate Parentheses
1 parent 7b1d8f5 commit 3b0951e

File tree

13 files changed

+338
-140
lines changed

13 files changed

+338
-140
lines changed

lcof2/剑指 Offer II 085. 生成匹配的括号/README.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,126 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
深度优先搜索 DFS。
43+
4244
<!-- tabs:start -->
4345

4446
### **Python3**
4547

4648
<!-- 这里可写当前语言的特殊实现逻辑 -->
4749

4850
```python
49-
51+
class Solution:
52+
def generateParenthesis(self, n: int) -> List[str]:
53+
ans = []
54+
55+
def dfs(left, right, t):
56+
if left == n and right == n:
57+
ans.append(t)
58+
return
59+
if left < n:
60+
dfs(left + 1, right, t + '(')
61+
if right < left:
62+
dfs(left, right + 1, t + ')')
63+
64+
dfs(0, 0, '')
65+
return ans
5066
```
5167

5268
### **Java**
5369

5470
<!-- 这里可写当前语言的特殊实现逻辑 -->
5571

5672
```java
73+
class Solution {
74+
public List<String> generateParenthesis(int n) {
75+
List<String> ans = new ArrayList<>();
76+
dfs(0, 0, n, "", ans);
77+
return ans;
78+
}
79+
80+
private void dfs(int left, int right, int n, String t, List<String> ans) {
81+
if (left == n && right == n) {
82+
ans.add(t);
83+
return;
84+
}
85+
if (left < n) {
86+
dfs(left + 1, right, n, t + "(", ans);
87+
}
88+
if (right < left) {
89+
dfs(left, right + 1, n, t + ")", ans);
90+
}
91+
}
92+
}
93+
```
94+
95+
### **TypeScript**
96+
97+
```ts
98+
function generateParenthesis(n: number): string[] {
99+
let ans = [];
100+
dfs(0, 0, n, '', ans);
101+
return ans;
102+
};
103+
104+
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
105+
if (left == n && right == n) {
106+
ans.push(t);
107+
return;
108+
}
109+
if (left < n) {
110+
dfs(left + 1, right, n, t + '(', ans);
111+
}
112+
if (right < left) {
113+
dfs(left, right + 1, n, t + ')', ans);
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<string> generateParenthesis(int n) {
124+
vector<string> ans;
125+
dfs(0, 0, n, "", ans);
126+
return ans;
127+
}
128+
129+
void dfs(int left, int right, int n, string t, vector<string>& ans) {
130+
if (left == n && right == n)
131+
{
132+
ans.push_back(t);
133+
return;
134+
}
135+
if (left < n) dfs(left + 1, right, n, t + "(", ans);
136+
if (right < left) dfs(left, right + 1, n, t + ")", ans);
137+
}
138+
};
139+
```
57140

141+
### **Go**
142+
143+
```go
144+
func generateParenthesis(n int) []string {
145+
var ans []string
146+
dfs(0, 0, n, "", &ans)
147+
return ans
148+
}
149+
150+
func dfs(left, right, n int, t string, ans *[]string) {
151+
if left == n && right == n {
152+
*ans = append(*ans, t)
153+
return
154+
}
155+
if left < n {
156+
dfs(left+1, right, n, t+"(", ans)
157+
}
158+
if right < left {
159+
dfs(left, right+1, n, t+")", ans)
160+
}
161+
}
58162
```
59163

60164
### **...**
Lines changed: 18 additions & 0 deletions
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+
};
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public List<String> generateParenthesis(int n) {
3+
List<String> ans = new ArrayList<>();
4+
dfs(0, 0, n, "", ans);
5+
return ans;
6+
}
7+
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);
11+
return;
12+
}
13+
if (left < n) {
14+
dfs(left + 1, right, n, t + "(", ans);
15+
}
16+
if (right < left) {
17+
dfs(left, right + 1, n, t + ")", ans);
18+
}
19+
}
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def generateParenthesis(self, n: int) -> List[str]:
3+
ans = []
4+
5+
def dfs(left, right, t):
6+
if left == n and right == n:
7+
ans.append(t)
8+
return
9+
if left < n:
10+
dfs(left + 1, right, t + '(')
11+
if right < left:
12+
dfs(left, right + 1, t + ')')
13+
14+
dfs(0, 0, '')
15+
return ans
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function generateParenthesis(n: number): string[] {
2+
let ans = [];
3+
dfs(0, 0, n, '', ans);
4+
return ans;
5+
};
6+
7+
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
8+
if (left == n && right == n) {
9+
ans.push(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+
}

solution/0000-0099/0022.Generate Parentheses/README.md

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40-
dfs
40+
深度优先搜索 DFS。
4141

4242
<!-- tabs:start -->
4343

@@ -48,18 +48,19 @@ dfs
4848
```python
4949
class Solution:
5050
def generateParenthesis(self, n: int) -> List[str]:
51-
def dfs(ans, l, r, n):
52-
if len(ans) == (n << 1):
53-
self.res.append(ans)
51+
ans = []
52+
53+
def dfs(left, right, t):
54+
if left == n and right == n:
55+
ans.append(t)
5456
return
55-
if l < n:
56-
dfs(ans + '(', l + 1, r, n)
57-
if r < l:
58-
dfs(ans + ')', l, r + 1, n)
59-
60-
self.res = []
61-
dfs('', 0, 0, n)
62-
return self.res
57+
if left < n:
58+
dfs(left + 1, right, t + '(')
59+
if right < left:
60+
dfs(left, right + 1, t + ')')
61+
62+
dfs(0, 0, '')
63+
return ans
6364
```
6465

6566
### **Java**
@@ -69,21 +70,21 @@ class Solution:
6970
```java
7071
class Solution {
7172
public List<String> generateParenthesis(int n) {
72-
List<String> res = new ArrayList<>();
73-
dfs(res, "", 0, 0, n);
74-
return res;
73+
List<String> ans = new ArrayList<>();
74+
dfs(0, 0, n, "", ans);
75+
return ans;
7576
}
7677

77-
private void dfs(List<String> res, String ans, int l, int r, int n) {
78-
if (ans.length() == (n << 1)) {
79-
res.add(ans);
78+
private void dfs(int left, int right, int n, String t, List<String> ans) {
79+
if (left == n && right == n) {
80+
ans.add(t);
8081
return;
8182
}
82-
if (l < n) {
83-
dfs(res, ans + "(", l + 1, r, n);
83+
if (left < n) {
84+
dfs(left + 1, right, n, t + "(", ans);
8485
}
85-
if (r < l) {
86-
dfs(res, ans + ")", l, r + 1, n);
86+
if (right < left) {
87+
dfs(left, right + 1, n, t + ")", ans);
8788
}
8889
}
8990
}
@@ -94,20 +95,20 @@ class Solution {
9495
```ts
9596
function generateParenthesis(n: number): string[] {
9697
let ans = [];
97-
dfs(n, 0, 0, '', ans);
98+
dfs(0, 0, n, '', ans);
9899
return ans;
99100
};
100101

101-
function dfs(n: number, left: number, right: number, str: string, ans: string[]) {
102-
if (str.length == 2 * n) {
103-
ans.push(str);
102+
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
103+
if (left == n && right == n) {
104+
ans.push(t);
104105
return;
105106
}
106107
if (left < n) {
107-
dfs(n, left + 1, right, str + '(', ans);
108+
dfs(left + 1, right, n, t + '(', ans);
108109
}
109110
if (right < left) {
110-
dfs(n, left, right + 1, str + ')', ans);
111+
dfs(left, right + 1, n, t + ')', ans);
111112
}
112113
}
113114
```
@@ -118,19 +119,19 @@ function dfs(n: number, left: number, right: number, str: string, ans: string[])
118119
class Solution {
119120
public:
120121
vector<string> generateParenthesis(int n) {
121-
vector<string> res;
122-
dfs(res, "", 0, 0, n);
123-
return res;
122+
vector<string> ans;
123+
dfs(0, 0, n, "", ans);
124+
return ans;
124125
}
125126

126-
private:
127-
void dfs(vector<string>& res, string ans, int l, int r, int n) {
128-
if (ans.size() == (n << 1)) {
129-
res.push_back(ans);
127+
void dfs(int left, int right, int n, string t, vector<string>& ans) {
128+
if (left == n && right == n)
129+
{
130+
ans.push_back(t);
130131
return;
131132
}
132-
if (l < n) dfs(res, ans + "(", l + 1, r, n);
133-
if (r < l) dfs(res, ans + ")", l, r + 1, n);
133+
if (left < n) dfs(left + 1, right, n, t + "(", ans);
134+
if (right < left) dfs(left, right + 1, n, t + ")", ans);
134135
}
135136
};
136137
```
@@ -139,21 +140,21 @@ private:
139140

140141
```go
141142
func generateParenthesis(n int) []string {
142-
res := new([]string)
143-
dfs(res, "", 0, 0, n)
144-
return *res
143+
var ans []string
144+
dfs(0, 0, n, "", &ans)
145+
return ans
145146
}
146147

147-
func dfs(res *[]string, ans string, l, r, n int) {
148-
if len(ans) == (n << 1) {
149-
*res = append(*res, ans)
148+
func dfs(left, right, n int, t string, ans *[]string) {
149+
if left == n && right == n {
150+
*ans = append(*ans, t)
150151
return
151152
}
152-
if l < n {
153-
dfs(res, ans+"(", l+1, r, n)
153+
if left < n {
154+
dfs(left+1, right, n, t+"(", ans)
154155
}
155-
if r < l {
156-
dfs(res, ans+")", l, r+1, n)
156+
if right < left {
157+
dfs(left, right+1, n, t+")", ans)
157158
}
158159
}
159160
```

0 commit comments

Comments
 (0)