Skip to content

Commit 6432728

Browse files
committed
feat: update solutions to lc/lcof2 problems
lc No.0022 & lcof2 No.085.Generate Parentheses
1 parent ed3b092 commit 6432728

File tree

11 files changed

+222
-215
lines changed

11 files changed

+222
-215
lines changed

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

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
```python
5050
class Solution:
5151
def generateParenthesis(self, n: int) -> List[str]:
52-
ans = []
53-
5452
def dfs(left, right, t):
5553
if left == n and right == n:
5654
ans.append(t)
@@ -59,7 +57,8 @@ class Solution:
5957
dfs(left + 1, right, t + '(')
6058
if right < left:
6159
dfs(left, right + 1, t + ')')
62-
60+
61+
ans = []
6362
dfs(0, 0, '')
6463
return ans
6564
```
@@ -96,22 +95,21 @@ class Solution {
9695
```ts
9796
function generateParenthesis(n: number): string[] {
9897
let ans = [];
99-
dfs(0, 0, n, "", ans);
98+
let dfs = function (left, right, t) {
99+
if (left == n && right == n) {
100+
ans.push(t);
101+
return;
102+
}
103+
if (left < n) {
104+
dfs(left + 1, right, t + '(');
105+
}
106+
if (right < left) {
107+
dfs(left, right + 1, t + ')');
108+
}
109+
};
110+
dfs(0, 0, '');
100111
return ans;
101112
}
102-
103-
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
104-
if (left == n && right == n) {
105-
ans.push(t);
106-
return;
107-
}
108-
if (left < n) {
109-
dfs(left + 1, right, n, t + "(", ans);
110-
}
111-
if (right < left) {
112-
dfs(left, right + 1, n, t + ")", ans);
113-
}
114-
}
115113
```
116114

117115
### **C++**
@@ -142,21 +140,21 @@ public:
142140
```go
143141
func generateParenthesis(n int) []string {
144142
var ans []string
145-
dfs(0, 0, n, "", &ans)
146-
return ans
147-
}
148-
149-
func dfs(left, right, n int, t string, ans *[]string) {
150-
if left == n && right == n {
151-
*ans = append(*ans, t)
152-
return
153-
}
154-
if left < n {
155-
dfs(left+1, right, n, t+"(", ans)
156-
}
157-
if right < left {
158-
dfs(left, right+1, n, t+")", ans)
143+
var dfs func(left, right int, t string)
144+
dfs = func(left, right int, t string) {
145+
if left == n && right == n {
146+
ans = append(ans, t)
147+
return
148+
}
149+
if left < n {
150+
dfs(left+1, right, t+"(")
151+
}
152+
if right < left {
153+
dfs(left, right+1, t+")")
154+
}
159155
}
156+
dfs(0, 0, "")
157+
return ans
160158
}
161159
```
162160

@@ -168,23 +166,22 @@ func dfs(left, right, n int, t string, ans *[]string) {
168166
* @return {string[]}
169167
*/
170168
var generateParenthesis = function (n) {
171-
let res = [];
172-
dfs(n, 0, 0, "", res);
173-
return res;
169+
let ans = [];
170+
let dfs = function (left, right, t) {
171+
if (left == n && right == n) {
172+
ans.push(t);
173+
return;
174+
}
175+
if (left < n) {
176+
dfs(left + 1, right, t + '(');
177+
}
178+
if (right < left) {
179+
dfs(left, right + 1, t + ')');
180+
}
181+
};
182+
dfs(0, 0, '');
183+
return ans;
174184
};
175-
176-
function dfs(n, left, right, prev, res) {
177-
if (left == n && right == n) {
178-
res.push(prev);
179-
return;
180-
}
181-
if (left < n) {
182-
dfs(n, left + 1, right, prev + "(", res);
183-
}
184-
if (right < left) {
185-
dfs(n, left, right + 1, prev + ")", res);
186-
}
187-
}
188185
```
189186

190187
### **...**
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
func generateParenthesis(n int) []string {
22
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)
3+
var dfs func(left, right int, t string)
4+
dfs = func(left, right int, t string) {
5+
if left == n && right == n {
6+
ans = append(ans, t)
7+
return
8+
}
9+
if left < n {
10+
dfs(left+1, right, t+"(")
11+
}
12+
if right < left {
13+
dfs(left, right+1, t+")")
14+
}
1715
}
16+
dfs(0, 0, "")
17+
return ans
1818
}

lcof2/剑指 Offer II 085. 生成匹配的括号/Solution.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
* @return {string[]}
44
*/
55
var generateParenthesis = function (n) {
6-
let res = [];
7-
dfs(n, 0, 0, "", res);
8-
return res;
6+
let ans = [];
7+
let dfs = function (left, right, t) {
8+
if (left == n && right == n) {
9+
ans.push(t);
10+
return;
11+
}
12+
if (left < n) {
13+
dfs(left + 1, right, t + '(');
14+
}
15+
if (right < left) {
16+
dfs(left, right + 1, t + ')');
17+
}
18+
};
19+
dfs(0, 0, '');
20+
return ans;
921
};
10-
11-
function dfs(n, left, right, prev, res) {
12-
if (left == n && right == n) {
13-
res.push(prev);
14-
return;
15-
}
16-
if (left < n) {
17-
dfs(n, left + 1, right, prev + "(", res);
18-
}
19-
if (right < left) {
20-
dfs(n, left, right + 1, prev + ")", res);
21-
}
22-
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
class Solution:
22
def generateParenthesis(self, n: int) -> List[str]:
3-
ans = []
4-
53
def dfs(left, right, t):
64
if left == n and right == n:
75
ans.append(t)
@@ -11,5 +9,6 @@ def dfs(left, right, t):
119
if right < left:
1210
dfs(left, right + 1, t + ')')
1311

12+
ans = []
1413
dfs(0, 0, '')
1514
return ans
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
function generateParenthesis(n: number): string[] {
22
let ans = [];
3-
dfs(0, 0, n, "", ans);
3+
let dfs = function (left, right, t) {
4+
if (left == n && right == n) {
5+
ans.push(t);
6+
return;
7+
}
8+
if (left < n) {
9+
dfs(left + 1, right, t + '(');
10+
}
11+
if (right < left) {
12+
dfs(left, right + 1, t + ')');
13+
}
14+
};
15+
dfs(0, 0, '');
416
return ans;
517
}
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: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@
4747
```python
4848
class Solution:
4949
def generateParenthesis(self, n: int) -> List[str]:
50-
ans = []
51-
5250
def dfs(left, right, t):
5351
if left == n and right == n:
5452
ans.append(t)
@@ -58,6 +56,7 @@ class Solution:
5856
if right < left:
5957
dfs(left, right + 1, t + ')')
6058

59+
ans = []
6160
dfs(0, 0, '')
6261
return ans
6362
```
@@ -94,22 +93,21 @@ class Solution {
9493
```ts
9594
function generateParenthesis(n: number): string[] {
9695
let ans = [];
97-
dfs(0, 0, n, "", ans);
96+
let dfs = function (left, right, t) {
97+
if (left == n && right == n) {
98+
ans.push(t);
99+
return;
100+
}
101+
if (left < n) {
102+
dfs(left + 1, right, t + '(');
103+
}
104+
if (right < left) {
105+
dfs(left, right + 1, t + ')');
106+
}
107+
};
108+
dfs(0, 0, '');
98109
return ans;
99110
}
100-
101-
function dfs(left: number, right: number, n: number, t: string, ans: string[]) {
102-
if (left == n && right == n) {
103-
ans.push(t);
104-
return;
105-
}
106-
if (left < n) {
107-
dfs(left + 1, right, n, t + "(", ans);
108-
}
109-
if (right < left) {
110-
dfs(left, right + 1, n, t + ")", ans);
111-
}
112-
}
113111
```
114112

115113
### **C++**
@@ -140,21 +138,21 @@ public:
140138
```go
141139
func generateParenthesis(n int) []string {
142140
var ans []string
143-
dfs(0, 0, n, "", &ans)
144-
return ans
145-
}
146-
147-
func dfs(left, right, n int, t string, ans *[]string) {
148-
if left == n && right == n {
149-
*ans = append(*ans, t)
150-
return
151-
}
152-
if left < n {
153-
dfs(left+1, right, n, t+"(", ans)
154-
}
155-
if right < left {
156-
dfs(left, right+1, n, t+")", ans)
141+
var dfs func(left, right int, t string)
142+
dfs = func(left, right int, t string) {
143+
if left == n && right == n {
144+
ans = append(ans, t)
145+
return
146+
}
147+
if left < n {
148+
dfs(left+1, right, t+"(")
149+
}
150+
if right < left {
151+
dfs(left, right+1, t+")")
152+
}
157153
}
154+
dfs(0, 0, "")
155+
return ans
158156
}
159157
```
160158

@@ -166,23 +164,22 @@ func dfs(left, right, n int, t string, ans *[]string) {
166164
* @return {string[]}
167165
*/
168166
var generateParenthesis = function (n) {
169-
let res = [];
170-
dfs(n, 0, 0, "", res);
171-
return res;
167+
let ans = [];
168+
let dfs = function (left, right, t) {
169+
if (left == n && right == n) {
170+
ans.push(t);
171+
return;
172+
}
173+
if (left < n) {
174+
dfs(left + 1, right, t + '(');
175+
}
176+
if (right < left) {
177+
dfs(left, right + 1, t + ')');
178+
}
179+
};
180+
dfs(0, 0, '');
181+
return ans;
172182
};
173-
174-
function dfs(n, left, right, prev, res) {
175-
if (left == n && right == n) {
176-
res.push(prev);
177-
return;
178-
}
179-
if (left < n) {
180-
dfs(n, left + 1, right, prev + "(", res);
181-
}
182-
if (right < left) {
183-
dfs(n, left, right + 1, prev + ")", res);
184-
}
185-
}
186183
```
187184

188185
### **...**

0 commit comments

Comments
 (0)