Skip to content

feat: update solutions to lc problems: No.16,22 #3112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0000-0099/0016.3Sum Closest/Solution.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function threeSumClosest(nums: number[], target: number): number {
nums.sort((a, b) => a - b);
let ans: number = 1 << 30;
let ans: number = Infinity;
const n = nums.length;
for (let i = 0; i < n; ++i) {
let j = i + 1;
Expand Down
17 changes: 0 additions & 17 deletions solution/0000-0099/0020.Valid Parentheses/solutions.cs

This file was deleted.

112 changes: 32 additions & 80 deletions solution/0000-0099/0022.Generate Parentheses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,27 +179,23 @@ function generateParenthesis(n: number): string[] {

```rust
impl Solution {
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
if left == 0 && right == 0 {
res.push(s.clone());
return;
}
if left > 0 {
s.push('(');
Self::dfs(left - 1, right, s, res);
s.pop();
}
if right > left {
s.push(')');
Self::dfs(left, right - 1, s, res);
s.pop();
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut ans = Vec::new();

fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
if l > n || r > n || l < r {
return;
}
if l == n && r == n {
ans.push(t);
return;
}
dfs(ans, l + 1, r, format!("{}(", t), n);
dfs(ans, l, r + 1, format!("{})", t), n);
}
}

pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut res = Vec::new();
Self::dfs(n, n, &mut String::new(), &mut res);
res
dfs(&mut ans, 0, 0, String::new(), n);
ans
}
}
```
Expand Down Expand Up @@ -229,75 +225,31 @@ var generateParenthesis = function (n) {
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut dp: Vec<Vec<String>> = vec![vec![]; n as usize + 1];

// Initialize the dp vector
dp[0].push(String::from(""));
dp[1].push(String::from("()"));

// Begin the actual dp process
for i in 2..=n as usize {
for j in 0..i as usize {
let dp_c = dp.clone();
let first_half = &dp_c[j];
let second_half = &dp_c[i - j - 1];

for f in first_half {
for s in second_half {
let f_c = f.clone();
let cur_str = f_c + "(" + &*s + ")";
dp[i].push(cur_str);
}
}
}
}

dp[n as usize].clone()
}
}
```

#### PHP

```php
class Solution {
/**
* @param int $n
* @return string[]
* @param Integer $n
* @return String[]
*/

function generateParenthesis($n) {
$result = [];
$this->backtrack($result, '', 0, 0, $n);
return $result;
}
$ans = [];

function backtrack(&$result, $current, $open, $close, $max) {
if (strlen($current) === $max * 2) {
$result[] = $current;
return;
}
if ($open < $max) {
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
}
if ($close < $open) {
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
}
$dfs = function ($l, $r, $t) use ($n, &$ans, &$dfs) {
if ($l > $n || $r > $n || $l < $r) {
return;
}
if ($l == $n && $r == $n) {
$ans[] = $t;
return;
}
$dfs($l + 1, $r, $t . '(');
$dfs($l, $r + 1, $t . ')');
};

$dfs(0, 0, '');
return $ans;
}
}
```
Expand Down
108 changes: 34 additions & 74 deletions solution/0000-0099/0022.Generate Parentheses/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,25 @@ func generateParenthesis(n int) (ans []string) {
#### TypeScript

```ts
function generateParenthesis(n: number): string[] {
function dfs(l, r, t) {
if (l > n || r > n || l < r) {
return;
}
if (l == n && r == n) {
ans.push(t);
return;
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut ans = Vec::new();

fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
if l > n || r > n || l < r {
return;
}
if l == n && r == n {
ans.push(t);
return;
}
dfs(ans, l + 1, r, format!("{}(", t), n);
dfs(ans, l, r + 1, format!("{})", t), n);
}
dfs(l + 1, r, t + '(');
dfs(l, r + 1, t + ')');

dfs(&mut ans, 0, 0, String::new(), n);
ans
}
let ans = [];
dfs(0, 0, '');
return ans;
}
```

Expand Down Expand Up @@ -220,75 +224,31 @@ var generateParenthesis = function (n) {
};
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### Rust

```rust
impl Solution {
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut dp: Vec<Vec<String>> = vec![vec![]; n as usize + 1];

// Initialize the dp vector
dp[0].push(String::from(""));
dp[1].push(String::from("()"));

// Begin the actual dp process
for i in 2..=n as usize {
for j in 0..i as usize {
let dp_c = dp.clone();
let first_half = &dp_c[j];
let second_half = &dp_c[i - j - 1];

for f in first_half {
for s in second_half {
let f_c = f.clone();
let cur_str = f_c + "(" + &*s + ")";
dp[i].push(cur_str);
}
}
}
}

dp[n as usize].clone()
}
}
```

#### PHP

```php
class Solution {
/**
* @param int $n
* @return string[]
* @param Integer $n
* @return String[]
*/

function generateParenthesis($n) {
$result = [];
$this->backtrack($result, '', 0, 0, $n);
return $result;
}
$ans = [];

function backtrack(&$result, $current, $open, $close, $max) {
if (strlen($current) === $max * 2) {
$result[] = $current;
return;
}
if ($open < $max) {
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
}
if ($close < $open) {
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
}
$dfs = function ($l, $r, $t) use ($n, &$ans, &$dfs) {
if ($l > $n || $r > $n || $l < $r) {
return;
}
if ($l == $n && $r == $n) {
$ans[] = $t;
return;
}
$dfs($l + 1, $r, $t . '(');
$dfs($l, $r + 1, $t . ')');
};

$dfs(0, 0, '');
return $ans;
}
}
```
Expand Down
36 changes: 17 additions & 19 deletions solution/0000-0099/0022.Generate Parentheses/Solution.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
class Solution {
/**
* @param int $n
* @return string[]
*/

function generateParenthesis($n) {
$result = [];
$this->backtrack($result, '', 0, 0, $n);
return $result;
}
/**
* @param Integer $n
* @return String[]
*/
function generateParenthesis($n) {
$ans = [];

function backtrack(&$result, $current, $open, $close, $max) {
if (strlen($current) === $max * 2) {
$result[] = $current;
$dfs = function($l, $r, $t) use ($n, &$ans, &$dfs) {
if ($l > $n || $r > $n || $l < $r) return;
if ($l == $n && $r == $n) {
$ans[] = $t;
return;
}
if ($open < $max) {
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
}
if ($close < $open) {
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
}
}
$dfs($l + 1, $r, $t . "(");
$dfs($l, $r + 1, $t . ")");
};

$dfs(0, 0, "");
return $ans;
}
}
34 changes: 15 additions & 19 deletions solution/0000-0099/0022.Generate Parentheses/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
impl Solution {
fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec<String>) {
if left == 0 && right == 0 {
res.push(s.clone());
return;
}
if left > 0 {
s.push('(');
Self::dfs(left - 1, right, s, res);
s.pop();
}
if right > left {
s.push(')');
Self::dfs(left, right - 1, s, res);
s.pop();
pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut ans = Vec::new();

fn dfs(ans: &mut Vec<String>, l: i32, r: i32, t: String, n: i32) {
if l > n || r > n || l < r {
return;
}
if l == n && r == n {
ans.push(t);
return;
}
dfs(ans, l + 1, r, format!("{}(", t), n);
dfs(ans, l, r + 1, format!("{})", t), n);
}
}

pub fn generate_parenthesis(n: i32) -> Vec<String> {
let mut res = Vec::new();
Self::dfs(n, n, &mut String::new(), &mut res);
res
dfs(&mut ans, 0, 0, String::new(), n);
ans
}
}
Loading
Loading