Skip to content
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

feat: update solutions to lc problems: No.11,15 #2468

Merged
merged 1 commit into from
Mar 20, 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
35 changes: 15 additions & 20 deletions solution/0000-0099/0011.Container With Most Water/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ impl Solution {
pub fn max_area(height: Vec<i32>) -> 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
}
}
```
Expand Down Expand Up @@ -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;
}
}
```
Expand Down
35 changes: 15 additions & 20 deletions solution/0000-0099/0011.Container With Most Water/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ impl Solution {
pub fn max_area(height: Vec<i32>) -> 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
}
}
```
Expand Down Expand Up @@ -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;
}
}
```
Expand Down
31 changes: 13 additions & 18 deletions solution/0000-0099/0011.Container With Most Water/Solution.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
6 changes: 3 additions & 3 deletions solution/0000-0099/0011.Container With Most Water/Solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ impl Solution {
pub fn max_area(height: Vec<i32>) -> 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
}
}
54 changes: 21 additions & 33 deletions solution/0000-0099/0015.3Sum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```
Expand Down
54 changes: 21 additions & 33 deletions solution/0000-0099/0015.3Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```
Expand Down
57 changes: 22 additions & 35 deletions solution/0000-0099/0015.3Sum/Solution.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Loading