From c7b0e899433b0797f9edea47c0cd4d5961a4e4b0 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 20 Mar 2024 10:04:42 +0800 Subject: [PATCH] feat: update solutions to lc problems: No.11,15 --- .../0011.Container With Most Water/README.md | 35 +++++------- .../README_EN.md | 35 +++++------- .../Solution.php | 31 +++++----- .../Solution.rs | 6 +- solution/0000-0099/0015.3Sum/README.md | 54 +++++++----------- solution/0000-0099/0015.3Sum/README_EN.md | 54 +++++++----------- solution/0000-0099/0015.3Sum/Solution.php | 57 +++++++------------ 7 files changed, 110 insertions(+), 162 deletions(-) diff --git a/solution/0000-0099/0011.Container With Most Water/README.md b/solution/0000-0099/0011.Container With Most Water/README.md index 6dbd8cf41fce7..16b0d2538533d 100644 --- a/solution/0000-0099/0011.Container With Most Water/README.md +++ b/solution/0000-0099/0011.Container With Most Water/README.md @@ -151,16 +151,16 @@ impl Solution { pub fn max_area(height: Vec) -> i32 { let mut i = 0; let mut j = height.len() - 1; - let mut res = 0; + let mut ans = 0; while i < j { - res = res.max(height[i].min(height[j]) * ((j - i) as i32)); + ans = ans.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { j -= 1; } } - res + ans } } ``` @@ -209,28 +209,23 @@ public class Solution { ```php class Solution { /** - * @param int[] $height - * @return int + * @param Integer[] $height + * @return Integer */ - function maxArea($height) { - $left = 0; - $right = count($height) - 1; - $maxArea = 0; - - while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); - - $maxArea = max($maxArea, $area); - - if ($height[$left] < $height[$right]) { - $left++; + $i = 0; + $j = count($height) - 1; + $ans = 0; + while ($i < $j) { + $t = min($height[$i], $height[$j]) * ($j - $i); + $ans = max($ans, $t); + if ($height[$i] < $height[$j]) { + ++$i; } else { - $right--; + --$j; } } - - return $maxArea; + return $ans; } } ``` diff --git a/solution/0000-0099/0011.Container With Most Water/README_EN.md b/solution/0000-0099/0011.Container With Most Water/README_EN.md index f321d2f5d264a..1fabfd07a1177 100644 --- a/solution/0000-0099/0011.Container With Most Water/README_EN.md +++ b/solution/0000-0099/0011.Container With Most Water/README_EN.md @@ -146,16 +146,16 @@ impl Solution { pub fn max_area(height: Vec) -> i32 { let mut i = 0; let mut j = height.len() - 1; - let mut res = 0; + let mut ans = 0; while i < j { - res = res.max(height[i].min(height[j]) * ((j - i) as i32)); + ans = ans.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { j -= 1; } } - res + ans } } ``` @@ -204,28 +204,23 @@ public class Solution { ```php class Solution { /** - * @param int[] $height - * @return int + * @param Integer[] $height + * @return Integer */ - function maxArea($height) { - $left = 0; - $right = count($height) - 1; - $maxArea = 0; - - while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); - - $maxArea = max($maxArea, $area); - - if ($height[$left] < $height[$right]) { - $left++; + $i = 0; + $j = count($height) - 1; + $ans = 0; + while ($i < $j) { + $t = min($height[$i], $height[$j]) * ($j - $i); + $ans = max($ans, $t); + if ($height[$i] < $height[$j]) { + ++$i; } else { - $right--; + --$j; } } - - return $maxArea; + return $ans; } } ``` diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.php b/solution/0000-0099/0011.Container With Most Water/Solution.php index 320347510d2fa..604a65fff5213 100644 --- a/solution/0000-0099/0011.Container With Most Water/Solution.php +++ b/solution/0000-0099/0011.Container With Most Water/Solution.php @@ -1,26 +1,21 @@ class Solution { /** - * @param int[] $height - * @return int + * @param Integer[] $height + * @return Integer */ - function maxArea($height) { - $left = 0; - $right = count($height) - 1; - $maxArea = 0; - - while ($left < $right) { - $area = min($height[$left], $height[$right]) * ($right - $left); - - $maxArea = max($maxArea, $area); - - if ($height[$left] < $height[$right]) { - $left++; + $i = 0; + $j = count($height) - 1; + $ans = 0; + while ($i < $j) { + $t = min($height[$i], $height[$j]) * ($j - $i); + $ans = max($ans, $t); + if ($height[$i] < $height[$j]) { + ++$i; } else { - $right--; + --$j; } } - - return $maxArea; + return $ans; } -} +} \ No newline at end of file diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.rs b/solution/0000-0099/0011.Container With Most Water/Solution.rs index 29ad6eb63d77a..b099e438e842e 100644 --- a/solution/0000-0099/0011.Container With Most Water/Solution.rs +++ b/solution/0000-0099/0011.Container With Most Water/Solution.rs @@ -2,15 +2,15 @@ impl Solution { pub fn max_area(height: Vec) -> i32 { let mut i = 0; let mut j = height.len() - 1; - let mut res = 0; + let mut ans = 0; while i < j { - res = res.max(height[i].min(height[j]) * ((j - i) as i32)); + ans = ans.max(height[i].min(height[j]) * ((j - i) as i32)); if height[i] <= height[j] { i += 1; } else { j -= 1; } } - res + ans } } diff --git a/solution/0000-0099/0015.3Sum/README.md b/solution/0000-0099/0015.3Sum/README.md index d18011fdbac16..bbd02ec86adf5 100644 --- a/solution/0000-0099/0015.3Sum/README.md +++ b/solution/0000-0099/0015.3Sum/README.md @@ -386,49 +386,37 @@ end ```php class Solution { /** - * @param int[] $nums - * @return int[][]; + * @param Integer[] $nums + * @return Integer[][] */ - function threeSum($nums) { - $result = []; - $n = count($nums); - sort($nums); - for ($i = 0; $i < $n - 2; $i++) { - if ($i > 0 && $nums[$i] === $nums[$i - 1]) { + $ans = []; + $n = count($nums); + for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) { + if ($i > 0 && $nums[$i] == $nums[$i - 1]) { continue; } - - $left = $i + 1; - $right = $n - 1; - - while ($left < $right) { - $sum = $nums[$i] + $nums[$left] + $nums[$right]; - - if ($sum === 0) { - $triplet = [$nums[$i], $nums[$left], $nums[$right]]; - $result[] = $triplet; - - while ($left < $right && $nums[$left] === $nums[$left + 1]) { - $left++; + $j = $i + 1; + $k = $n - 1; + while ($j < $k) { + $x = $nums[$i] + $nums[$j] + $nums[$k]; + if ($x < 0) { + ++$j; + } elseif ($x > 0) { + --$k; + } else { + $ans[] = [$nums[$i], $nums[$j++], $nums[$k--]]; + while ($j < $k && $nums[$j] == $nums[$j - 1]) { + ++$j; } - - while ($left < $right && $nums[$right] === $nums[$right - 1]) { - $right--; + while ($j < $k && $nums[$k] == $nums[$k + 1]) { + --$k; } - - $left++; - $right--; - } elseif ($sum < 0) { - $left++; - } else { - $right--; } } } - - return $result; + return $ans; } } ``` diff --git a/solution/0000-0099/0015.3Sum/README_EN.md b/solution/0000-0099/0015.3Sum/README_EN.md index 1c8d6e7012ba9..02097392c6c66 100644 --- a/solution/0000-0099/0015.3Sum/README_EN.md +++ b/solution/0000-0099/0015.3Sum/README_EN.md @@ -378,49 +378,37 @@ end ```php class Solution { /** - * @param int[] $nums - * @return int[][]; + * @param Integer[] $nums + * @return Integer[][] */ - function threeSum($nums) { - $result = []; - $n = count($nums); - sort($nums); - for ($i = 0; $i < $n - 2; $i++) { - if ($i > 0 && $nums[$i] === $nums[$i - 1]) { + $ans = []; + $n = count($nums); + for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) { + if ($i > 0 && $nums[$i] == $nums[$i - 1]) { continue; } - - $left = $i + 1; - $right = $n - 1; - - while ($left < $right) { - $sum = $nums[$i] + $nums[$left] + $nums[$right]; - - if ($sum === 0) { - $triplet = [$nums[$i], $nums[$left], $nums[$right]]; - $result[] = $triplet; - - while ($left < $right && $nums[$left] === $nums[$left + 1]) { - $left++; + $j = $i + 1; + $k = $n - 1; + while ($j < $k) { + $x = $nums[$i] + $nums[$j] + $nums[$k]; + if ($x < 0) { + ++$j; + } elseif ($x > 0) { + --$k; + } else { + $ans[] = [$nums[$i], $nums[$j++], $nums[$k--]]; + while ($j < $k && $nums[$j] == $nums[$j - 1]) { + ++$j; } - - while ($left < $right && $nums[$right] === $nums[$right - 1]) { - $right--; + while ($j < $k && $nums[$k] == $nums[$k + 1]) { + --$k; } - - $left++; - $right--; - } elseif ($sum < 0) { - $left++; - } else { - $right--; } } } - - return $result; + return $ans; } } ``` diff --git a/solution/0000-0099/0015.3Sum/Solution.php b/solution/0000-0099/0015.3Sum/Solution.php index 64ef0f8c9cd8c..cd4ebfb649f5f 100644 --- a/solution/0000-0099/0015.3Sum/Solution.php +++ b/solution/0000-0099/0015.3Sum/Solution.php @@ -1,48 +1,35 @@ class Solution { /** - * @param int[] $nums - * @return int[][]; + * @param Integer[] $nums + * @return Integer[][] */ - function threeSum($nums) { - $result = []; - $n = count($nums); - sort($nums); - for ($i = 0; $i < $n - 2; $i++) { - - if ($i > 0 && $nums[$i] === $nums[$i - 1]) { + $ans = []; + $n = count($nums); + for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) { + if ($i > 0 && $nums[$i] == $nums[$i - 1]) { continue; } - - $left = $i + 1; - $right = $n - 1; - - while ($left < $right) { - $sum = $nums[$i] + $nums[$left] + $nums[$right]; - - if ($sum === 0) { - $triplet = array($nums[$i], $nums[$left], $nums[$right]); - $result[] = $triplet; - - while ($left < $right && $nums[$left] === $nums[$left + 1]) { - $left++; + $j = $i + 1; + $k = $n - 1; + while ($j < $k) { + $x = $nums[$i] + $nums[$j] + $nums[$k]; + if ($x < 0) { + ++$j; + } elseif ($x > 0) { + --$k; + } else { + $ans[] = [$nums[$i], $nums[$j++], $nums[$k--]]; + while ($j < $k && $nums[$j] == $nums[$j - 1]) { + ++$j; } - - while ($left < $right && $nums[$right] === $nums[$right - 1]) { - $right--; + while ($j < $k && $nums[$k] == $nums[$k + 1]) { + --$k; } - - $left++; - $right--; - } elseif ($sum < 0) { - $left++; - } else { - $right--; } } } - - return $result; + return $ans; } -} +} \ No newline at end of file