diff --git a/solution/0000-0099/0016.3Sum Closest/Solution.ts b/solution/0000-0099/0016.3Sum Closest/Solution.ts index e5c62987bd306..f569596ea9ee4 100644 --- a/solution/0000-0099/0016.3Sum Closest/Solution.ts +++ b/solution/0000-0099/0016.3Sum Closest/Solution.ts @@ -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; diff --git a/solution/0000-0099/0020.Valid Parentheses/solutions.cs b/solution/0000-0099/0020.Valid Parentheses/solutions.cs deleted file mode 100644 index f0b79f30fbc19..0000000000000 --- a/solution/0000-0099/0020.Valid Parentheses/solutions.cs +++ /dev/null @@ -1,17 +0,0 @@ -public class Solution { - public bool IsValid(string s) { - Stack stk = new Stack(); - foreach (var c in s.ToCharArray()) { - if (c == '(') { - stk.Push(')'); - } else if (c == '[') { - stk.Push(']'); - } else if (c == '{') { - stk.Push('}'); - } else if (stk.Count == 0 || stk.Pop() != c) { - return false; - } - } - return stk.Count == 0; - } -} diff --git a/solution/0000-0099/0022.Generate Parentheses/README.md b/solution/0000-0099/0022.Generate Parentheses/README.md index ef5af9be22087..ed6a2a137616d 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README.md +++ b/solution/0000-0099/0022.Generate Parentheses/README.md @@ -179,27 +179,23 @@ function generateParenthesis(n: number): string[] { ```rust impl Solution { - fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec) { - 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 { + let mut ans = Vec::new(); + + fn dfs(ans: &mut Vec, 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 { - let mut res = Vec::new(); - Self::dfs(n, n, &mut String::new(), &mut res); - res + dfs(&mut ans, 0, 0, String::new(), n); + ans } } ``` @@ -229,75 +225,31 @@ var generateParenthesis = function (n) { }; ``` - - - - - - -### 方法二 - - - -#### Rust - -```rust -impl Solution { - pub fn generate_parenthesis(n: i32) -> Vec { - let mut dp: Vec> = 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; } } ``` diff --git a/solution/0000-0099/0022.Generate Parentheses/README_EN.md b/solution/0000-0099/0022.Generate Parentheses/README_EN.md index aea8581904098..1e658a09e2759 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README_EN.md +++ b/solution/0000-0099/0022.Generate Parentheses/README_EN.md @@ -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 { + let mut ans = Vec::new(); + + fn dfs(ans: &mut Vec, 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; } ``` @@ -220,75 +224,31 @@ var generateParenthesis = function (n) { }; ``` - - - - - - -### Solution 2 - - - -#### Rust - -```rust -impl Solution { - pub fn generate_parenthesis(n: i32) -> Vec { - let mut dp: Vec> = 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; } } ``` diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution.php b/solution/0000-0099/0022.Generate Parentheses/Solution.php index a172fd488bcac..3cf2a16651fe9 100644 --- a/solution/0000-0099/0022.Generate Parentheses/Solution.php +++ b/solution/0000-0099/0022.Generate Parentheses/Solution.php @@ -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; +} } diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution.rs b/solution/0000-0099/0022.Generate Parentheses/Solution.rs index 74c4c9bbdeb84..aeafe6e7cad0b 100644 --- a/solution/0000-0099/0022.Generate Parentheses/Solution.rs +++ b/solution/0000-0099/0022.Generate Parentheses/Solution.rs @@ -1,24 +1,20 @@ impl Solution { - fn dfs(left: i32, right: i32, s: &mut String, res: &mut Vec) { - 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 { + let mut ans = Vec::new(); + + fn dfs(ans: &mut Vec, 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 { - let mut res = Vec::new(); - Self::dfs(n, n, &mut String::new(), &mut res); - res + dfs(&mut ans, 0, 0, String::new(), n); + ans } } diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution2.rs b/solution/0000-0099/0022.Generate Parentheses/Solution2.rs deleted file mode 100644 index c5c8acd79026b..0000000000000 --- a/solution/0000-0099/0022.Generate Parentheses/Solution2.rs +++ /dev/null @@ -1,28 +0,0 @@ -impl Solution { - pub fn generate_parenthesis(n: i32) -> Vec { - let mut dp: Vec> = 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() - } -} diff --git a/solution/0500-0599/0521.Longest Uncommon Subsequence I/README.md b/solution/0500-0599/0521.Longest Uncommon Subsequence I/README.md index 51c52e939cf4e..e1cfb73170823 100644 --- a/solution/0500-0599/0521.Longest Uncommon Subsequence I/README.md +++ b/solution/0500-0599/0521.Longest Uncommon Subsequence I/README.md @@ -66,7 +66,11 @@ tags: -### 方法一 +### 方法一:脑筋急转弯 + +如果字符串 `a` 和 `b` 相等,那么它们没有特殊序列,返回 `-1`;否则,返回长度较长的字符串的长度。 + +时间复杂度 $O(n)$,其中 $n$ 为字符串 `a` 和 `b` 中较长的字符串的长度。空间复杂度 $O(1)$。 @@ -117,7 +121,7 @@ func findLUSlength(a string, b string) int { ```ts function findLUSlength(a: string, b: string): number { - return a != b ? Math.max(a.length, b.length) : -1; + return a === b ? -1 : Math.max(a.length, b.length); } ``` diff --git a/solution/0500-0599/0521.Longest Uncommon Subsequence I/README_EN.md b/solution/0500-0599/0521.Longest Uncommon Subsequence I/README_EN.md index adf088dfbfa6d..4d42cf26f4479 100644 --- a/solution/0500-0599/0521.Longest Uncommon Subsequence I/README_EN.md +++ b/solution/0500-0599/0521.Longest Uncommon Subsequence I/README_EN.md @@ -60,7 +60,11 @@ Note that "cdc" is also a longest uncommon subsequence. -### Solution 1 +### Solution 1: Quick Thinking + +If strings `a` and `b` are equal, then they have no special sequences, return `-1`; otherwise, return the length of the longer string. + +The time complexity is $O(n)$, where $n$ is the length of the longer string among `a` and `b`. The space complexity is $O(1)$. @@ -111,7 +115,7 @@ func findLUSlength(a string, b string) int { ```ts function findLUSlength(a: string, b: string): number { - return a != b ? Math.max(a.length, b.length) : -1; + return a === b ? -1 : Math.max(a.length, b.length); } ``` diff --git a/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.ts b/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.ts index f2abac3c4b2ea..4edfbe8cddf1d 100644 --- a/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.ts +++ b/solution/0500-0599/0521.Longest Uncommon Subsequence I/Solution.ts @@ -1,3 +1,3 @@ function findLUSlength(a: string, b: string): number { - return a != b ? Math.max(a.length, b.length) : -1; + return a === b ? -1 : Math.max(a.length, b.length); }