Skip to content

Commit 1c9b2ab

Browse files
authored
feat: update solutions to lc problems: No.16,22 (#3112)
1 parent 910a556 commit 1c9b2ab

File tree

10 files changed

+112
-243
lines changed

10 files changed

+112
-243
lines changed

solution/0000-0099/0016.3Sum Closest/Solution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function threeSumClosest(nums: number[], target: number): number {
22
nums.sort((a, b) => a - b);
3-
let ans: number = 1 << 30;
3+
let ans: number = Infinity;
44
const n = nums.length;
55
for (let i = 0; i < n; ++i) {
66
let j = i + 1;

solution/0000-0099/0020.Valid Parentheses/solutions.cs

-17
This file was deleted.

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

+32-80
Original file line numberDiff line numberDiff line change
@@ -179,27 +179,23 @@ function generateParenthesis(n: number): string[] {
179179

180180
```rust
181181
impl Solution {
182-
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
183-
if left == 0 && right == 0 {
184-
res.push(s.clone());
185-
return;
186-
}
187-
if left > 0 {
188-
s.push('(');
189-
Self::dfs(left - 1, right, s, res);
190-
s.pop();
191-
}
192-
if right > left {
193-
s.push(')');
194-
Self::dfs(left, right - 1, s, res);
195-
s.pop();
182+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
183+
let mut ans = Vec::new();
184+
185+
fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
186+
if l > n || r > n || l < r {
187+
return;
188+
}
189+
if l == n && r == n {
190+
ans.push(t);
191+
return;
192+
}
193+
dfs(ans, l + 1, r, format!("{}(", t), n);
194+
dfs(ans, l, r + 1, format!("{})", t), n);
196195
}
197-
}
198196

199-
pub fn generate_parenthesis(n: i32) -> Vec<String> {
200-
let mut res = Vec::new();
201-
Self::dfs(n, n, &mut String::new(), &mut res);
202-
res
197+
dfs(&mut ans, 0, 0, String::new(), n);
198+
ans
203199
}
204200
}
205201
```
@@ -229,75 +225,31 @@ var generateParenthesis = function (n) {
229225
};
230226
```
231227

232-
<!-- tabs:end -->
233-
234-
<!-- solution:end -->
235-
236-
<!-- solution:start -->
237-
238-
### 方法二
239-
240-
<!-- tabs:start -->
241-
242-
#### Rust
243-
244-
```rust
245-
impl Solution {
246-
pub fn generate_parenthesis(n: i32) -> Vec<String> {
247-
let mut dp: Vec<Vec<String>> = vec![vec![]; n as usize + 1];
248-
249-
// Initialize the dp vector
250-
dp[0].push(String::from(""));
251-
dp[1].push(String::from("()"));
252-
253-
// Begin the actual dp process
254-
for i in 2..=n as usize {
255-
for j in 0..i as usize {
256-
let dp_c = dp.clone();
257-
let first_half = &dp_c[j];
258-
let second_half = &dp_c[i - j - 1];
259-
260-
for f in first_half {
261-
for s in second_half {
262-
let f_c = f.clone();
263-
let cur_str = f_c + "(" + &*s + ")";
264-
dp[i].push(cur_str);
265-
}
266-
}
267-
}
268-
}
269-
270-
dp[n as usize].clone()
271-
}
272-
}
273-
```
274-
275228
#### PHP
276229

277230
```php
278231
class Solution {
279232
/**
280-
* @param int $n
281-
* @return string[]
233+
* @param Integer $n
234+
* @return String[]
282235
*/
283-
284236
function generateParenthesis($n) {
285-
$result = [];
286-
$this->backtrack($result, '', 0, 0, $n);
287-
return $result;
288-
}
237+
$ans = [];
289238

290-
function backtrack(&$result, $current, $open, $close, $max) {
291-
if (strlen($current) === $max * 2) {
292-
$result[] = $current;
293-
return;
294-
}
295-
if ($open < $max) {
296-
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
297-
}
298-
if ($close < $open) {
299-
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
300-
}
239+
$dfs = function ($l, $r, $t) use ($n, &$ans, &$dfs) {
240+
if ($l > $n || $r > $n || $l < $r) {
241+
return;
242+
}
243+
if ($l == $n && $r == $n) {
244+
$ans[] = $t;
245+
return;
246+
}
247+
$dfs($l + 1, $r, $t . '(');
248+
$dfs($l, $r + 1, $t . ')');
249+
};
250+
251+
$dfs(0, 0, '');
252+
return $ans;
301253
}
302254
}
303255
```

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

+34-74
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,25 @@ func generateParenthesis(n int) (ans []string) {
148148
#### TypeScript
149149

150150
```ts
151-
function generateParenthesis(n: number): string[] {
152-
function dfs(l, r, t) {
153-
if (l > n || r > n || l < r) {
154-
return;
155-
}
156-
if (l == n && r == n) {
157-
ans.push(t);
158-
return;
151+
impl Solution {
152+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
153+
let mut ans = Vec::new();
154+
155+
fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
156+
if l > n || r > n || l < r {
157+
return;
158+
}
159+
if l == n && r == n {
160+
ans.push(t);
161+
return;
162+
}
163+
dfs(ans, l + 1, r, format!("{}(", t), n);
164+
dfs(ans, l, r + 1, format!("{})", t), n);
159165
}
160-
dfs(l + 1, r, t + '(');
161-
dfs(l, r + 1, t + ')');
166+
167+
dfs(&mut ans, 0, 0, String::new(), n);
168+
ans
162169
}
163-
let ans = [];
164-
dfs(0, 0, '');
165-
return ans;
166170
}
167171
```
168172

@@ -220,75 +224,31 @@ var generateParenthesis = function (n) {
220224
};
221225
```
222226

223-
<!-- tabs:end -->
224-
225-
<!-- solution:end -->
226-
227-
<!-- solution:start -->
228-
229-
### Solution 2
230-
231-
<!-- tabs:start -->
232-
233-
#### Rust
234-
235-
```rust
236-
impl Solution {
237-
pub fn generate_parenthesis(n: i32) -> Vec<String> {
238-
let mut dp: Vec<Vec<String>> = vec![vec![]; n as usize + 1];
239-
240-
// Initialize the dp vector
241-
dp[0].push(String::from(""));
242-
dp[1].push(String::from("()"));
243-
244-
// Begin the actual dp process
245-
for i in 2..=n as usize {
246-
for j in 0..i as usize {
247-
let dp_c = dp.clone();
248-
let first_half = &dp_c[j];
249-
let second_half = &dp_c[i - j - 1];
250-
251-
for f in first_half {
252-
for s in second_half {
253-
let f_c = f.clone();
254-
let cur_str = f_c + "(" + &*s + ")";
255-
dp[i].push(cur_str);
256-
}
257-
}
258-
}
259-
}
260-
261-
dp[n as usize].clone()
262-
}
263-
}
264-
```
265-
266227
#### PHP
267228

268229
```php
269230
class Solution {
270231
/**
271-
* @param int $n
272-
* @return string[]
232+
* @param Integer $n
233+
* @return String[]
273234
*/
274-
275235
function generateParenthesis($n) {
276-
$result = [];
277-
$this->backtrack($result, '', 0, 0, $n);
278-
return $result;
279-
}
236+
$ans = [];
280237

281-
function backtrack(&$result, $current, $open, $close, $max) {
282-
if (strlen($current) === $max * 2) {
283-
$result[] = $current;
284-
return;
285-
}
286-
if ($open < $max) {
287-
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
288-
}
289-
if ($close < $open) {
290-
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
291-
}
238+
$dfs = function ($l, $r, $t) use ($n, &$ans, &$dfs) {
239+
if ($l > $n || $r > $n || $l < $r) {
240+
return;
241+
}
242+
if ($l == $n && $r == $n) {
243+
$ans[] = $t;
244+
return;
245+
}
246+
$dfs($l + 1, $r, $t . '(');
247+
$dfs($l, $r + 1, $t . ')');
248+
};
249+
250+
$dfs(0, 0, '');
251+
return $ans;
292252
}
293253
}
294254
```
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
class Solution {
2-
/**
3-
* @param int $n
4-
* @return string[]
5-
*/
62

7-
function generateParenthesis($n) {
8-
$result = [];
9-
$this->backtrack($result, '', 0, 0, $n);
10-
return $result;
11-
}
3+
/**
4+
* @param Integer $n
5+
* @return String[]
6+
*/
7+
function generateParenthesis($n) {
8+
$ans = [];
129

13-
function backtrack(&$result, $current, $open, $close, $max) {
14-
if (strlen($current) === $max * 2) {
15-
$result[] = $current;
10+
$dfs = function($l, $r, $t) use ($n, &$ans, &$dfs) {
11+
if ($l > $n || $r > $n || $l < $r) return;
12+
if ($l == $n && $r == $n) {
13+
$ans[] = $t;
1614
return;
1715
}
18-
if ($open < $max) {
19-
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
20-
}
21-
if ($close < $open) {
22-
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
23-
}
24-
}
16+
$dfs($l + 1, $r, $t . "(");
17+
$dfs($l, $r + 1, $t . ")");
18+
};
19+
20+
$dfs(0, 0, "");
21+
return $ans;
22+
}
2523
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
impl Solution {
2-
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
3-
if left == 0 && right == 0 {
4-
res.push(s.clone());
5-
return;
6-
}
7-
if left > 0 {
8-
s.push('(');
9-
Self::dfs(left - 1, right, s, res);
10-
s.pop();
11-
}
12-
if right > left {
13-
s.push(')');
14-
Self::dfs(left, right - 1, s, res);
15-
s.pop();
2+
pub fn generate_parenthesis(n: i32) -> Vec<String> {
3+
let mut ans = Vec::new();
4+
5+
fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
6+
if l > n || r > n || l < r {
7+
return;
8+
}
9+
if l == n && r == n {
10+
ans.push(t);
11+
return;
12+
}
13+
dfs(ans, l + 1, r, format!("{}(", t), n);
14+
dfs(ans, l, r + 1, format!("{})", t), n);
1615
}
17-
}
1816

19-
pub fn generate_parenthesis(n: i32) -> Vec<String> {
20-
let mut res = Vec::new();
21-
Self::dfs(n, n, &mut String::new(), &mut res);
22-
res
17+
dfs(&mut ans, 0, 0, String::new(), n);
18+
ans
2319
}
2420
}

0 commit comments

Comments
 (0)