Skip to content

Commit 661ab46

Browse files
authored
feat: update solutions to lc problems: No.11,15 (#2468)
1 parent c78aa1e commit 661ab46

File tree

7 files changed

+110
-162
lines changed

7 files changed

+110
-162
lines changed

solution/0000-0099/0011.Container With Most Water/README.md

+15-20
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,16 @@ impl Solution {
151151
pub fn max_area(height: Vec<i32>) -> i32 {
152152
let mut i = 0;
153153
let mut j = height.len() - 1;
154-
let mut res = 0;
154+
let mut ans = 0;
155155
while i < j {
156-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
156+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
157157
if height[i] <= height[j] {
158158
i += 1;
159159
} else {
160160
j -= 1;
161161
}
162162
}
163-
res
163+
ans
164164
}
165165
}
166166
```
@@ -209,28 +209,23 @@ public class Solution {
209209
```php
210210
class Solution {
211211
/**
212-
* @param int[] $height
213-
* @return int
212+
* @param Integer[] $height
213+
* @return Integer
214214
*/
215-
216215
function maxArea($height) {
217-
$left = 0;
218-
$right = count($height) - 1;
219-
$maxArea = 0;
220-
221-
while ($left < $right) {
222-
$area = min($height[$left], $height[$right]) * ($right - $left);
223-
224-
$maxArea = max($maxArea, $area);
225-
226-
if ($height[$left] < $height[$right]) {
227-
$left++;
216+
$i = 0;
217+
$j = count($height) - 1;
218+
$ans = 0;
219+
while ($i < $j) {
220+
$t = min($height[$i], $height[$j]) * ($j - $i);
221+
$ans = max($ans, $t);
222+
if ($height[$i] < $height[$j]) {
223+
++$i;
228224
} else {
229-
$right--;
225+
--$j;
230226
}
231227
}
232-
233-
return $maxArea;
228+
return $ans;
234229
}
235230
}
236231
```

solution/0000-0099/0011.Container With Most Water/README_EN.md

+15-20
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ impl Solution {
146146
pub fn max_area(height: Vec<i32>) -> i32 {
147147
let mut i = 0;
148148
let mut j = height.len() - 1;
149-
let mut res = 0;
149+
let mut ans = 0;
150150
while i < j {
151-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
151+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
152152
if height[i] <= height[j] {
153153
i += 1;
154154
} else {
155155
j -= 1;
156156
}
157157
}
158-
res
158+
ans
159159
}
160160
}
161161
```
@@ -204,28 +204,23 @@ public class Solution {
204204
```php
205205
class Solution {
206206
/**
207-
* @param int[] $height
208-
* @return int
207+
* @param Integer[] $height
208+
* @return Integer
209209
*/
210-
211210
function maxArea($height) {
212-
$left = 0;
213-
$right = count($height) - 1;
214-
$maxArea = 0;
215-
216-
while ($left < $right) {
217-
$area = min($height[$left], $height[$right]) * ($right - $left);
218-
219-
$maxArea = max($maxArea, $area);
220-
221-
if ($height[$left] < $height[$right]) {
222-
$left++;
211+
$i = 0;
212+
$j = count($height) - 1;
213+
$ans = 0;
214+
while ($i < $j) {
215+
$t = min($height[$i], $height[$j]) * ($j - $i);
216+
$ans = max($ans, $t);
217+
if ($height[$i] < $height[$j]) {
218+
++$i;
223219
} else {
224-
$right--;
220+
--$j;
225221
}
226222
}
227-
228-
return $maxArea;
223+
return $ans;
229224
}
230225
}
231226
```
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
11
class Solution {
22
/**
3-
* @param int[] $height
4-
* @return int
3+
* @param Integer[] $height
4+
* @return Integer
55
*/
6-
76
function maxArea($height) {
8-
$left = 0;
9-
$right = count($height) - 1;
10-
$maxArea = 0;
11-
12-
while ($left < $right) {
13-
$area = min($height[$left], $height[$right]) * ($right - $left);
14-
15-
$maxArea = max($maxArea, $area);
16-
17-
if ($height[$left] < $height[$right]) {
18-
$left++;
7+
$i = 0;
8+
$j = count($height) - 1;
9+
$ans = 0;
10+
while ($i < $j) {
11+
$t = min($height[$i], $height[$j]) * ($j - $i);
12+
$ans = max($ans, $t);
13+
if ($height[$i] < $height[$j]) {
14+
++$i;
1915
} else {
20-
$right--;
16+
--$j;
2117
}
2218
}
23-
24-
return $maxArea;
19+
return $ans;
2520
}
26-
}
21+
}

solution/0000-0099/0011.Container With Most Water/Solution.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ impl Solution {
22
pub fn max_area(height: Vec<i32>) -> i32 {
33
let mut i = 0;
44
let mut j = height.len() - 1;
5-
let mut res = 0;
5+
let mut ans = 0;
66
while i < j {
7-
res = res.max(height[i].min(height[j]) * ((j - i) as i32));
7+
ans = ans.max(height[i].min(height[j]) * ((j - i) as i32));
88
if height[i] <= height[j] {
99
i += 1;
1010
} else {
1111
j -= 1;
1212
}
1313
}
14-
res
14+
ans
1515
}
1616
}

solution/0000-0099/0015.3Sum/README.md

+21-33
Original file line numberDiff line numberDiff line change
@@ -386,49 +386,37 @@ end
386386
```php
387387
class Solution {
388388
/**
389-
* @param int[] $nums
390-
* @return int[][];
389+
* @param Integer[] $nums
390+
* @return Integer[][]
391391
*/
392-
393392
function threeSum($nums) {
394-
$result = [];
395-
$n = count($nums);
396-
397393
sort($nums);
398-
for ($i = 0; $i < $n - 2; $i++) {
399-
if ($i > 0 && $nums[$i] === $nums[$i - 1]) {
394+
$ans = [];
395+
$n = count($nums);
396+
for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) {
397+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
400398
continue;
401399
}
402-
403-
$left = $i + 1;
404-
$right = $n - 1;
405-
406-
while ($left < $right) {
407-
$sum = $nums[$i] + $nums[$left] + $nums[$right];
408-
409-
if ($sum === 0) {
410-
$triplet = [$nums[$i], $nums[$left], $nums[$right]];
411-
$result[] = $triplet;
412-
413-
while ($left < $right && $nums[$left] === $nums[$left + 1]) {
414-
$left++;
400+
$j = $i + 1;
401+
$k = $n - 1;
402+
while ($j < $k) {
403+
$x = $nums[$i] + $nums[$j] + $nums[$k];
404+
if ($x < 0) {
405+
++$j;
406+
} elseif ($x > 0) {
407+
--$k;
408+
} else {
409+
$ans[] = [$nums[$i], $nums[$j++], $nums[$k--]];
410+
while ($j < $k && $nums[$j] == $nums[$j - 1]) {
411+
++$j;
415412
}
416-
417-
while ($left < $right && $nums[$right] === $nums[$right - 1]) {
418-
$right--;
413+
while ($j < $k && $nums[$k] == $nums[$k + 1]) {
414+
--$k;
419415
}
420-
421-
$left++;
422-
$right--;
423-
} elseif ($sum < 0) {
424-
$left++;
425-
} else {
426-
$right--;
427416
}
428417
}
429418
}
430-
431-
return $result;
419+
return $ans;
432420
}
433421
}
434422
```

solution/0000-0099/0015.3Sum/README_EN.md

+21-33
Original file line numberDiff line numberDiff line change
@@ -378,49 +378,37 @@ end
378378
```php
379379
class Solution {
380380
/**
381-
* @param int[] $nums
382-
* @return int[][];
381+
* @param Integer[] $nums
382+
* @return Integer[][]
383383
*/
384-
385384
function threeSum($nums) {
386-
$result = [];
387-
$n = count($nums);
388-
389385
sort($nums);
390-
for ($i = 0; $i < $n - 2; $i++) {
391-
if ($i > 0 && $nums[$i] === $nums[$i - 1]) {
386+
$ans = [];
387+
$n = count($nums);
388+
for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) {
389+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
392390
continue;
393391
}
394-
395-
$left = $i + 1;
396-
$right = $n - 1;
397-
398-
while ($left < $right) {
399-
$sum = $nums[$i] + $nums[$left] + $nums[$right];
400-
401-
if ($sum === 0) {
402-
$triplet = [$nums[$i], $nums[$left], $nums[$right]];
403-
$result[] = $triplet;
404-
405-
while ($left < $right && $nums[$left] === $nums[$left + 1]) {
406-
$left++;
392+
$j = $i + 1;
393+
$k = $n - 1;
394+
while ($j < $k) {
395+
$x = $nums[$i] + $nums[$j] + $nums[$k];
396+
if ($x < 0) {
397+
++$j;
398+
} elseif ($x > 0) {
399+
--$k;
400+
} else {
401+
$ans[] = [$nums[$i], $nums[$j++], $nums[$k--]];
402+
while ($j < $k && $nums[$j] == $nums[$j - 1]) {
403+
++$j;
407404
}
408-
409-
while ($left < $right && $nums[$right] === $nums[$right - 1]) {
410-
$right--;
405+
while ($j < $k && $nums[$k] == $nums[$k + 1]) {
406+
--$k;
411407
}
412-
413-
$left++;
414-
$right--;
415-
} elseif ($sum < 0) {
416-
$left++;
417-
} else {
418-
$right--;
419408
}
420409
}
421410
}
422-
423-
return $result;
411+
return $ans;
424412
}
425413
}
426414
```
+22-35
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,35 @@
11
class Solution {
22
/**
3-
* @param int[] $nums
4-
* @return int[][];
3+
* @param Integer[] $nums
4+
* @return Integer[][]
55
*/
6-
76
function threeSum($nums) {
8-
$result = [];
9-
$n = count($nums);
10-
117
sort($nums);
12-
for ($i = 0; $i < $n - 2; $i++) {
13-
14-
if ($i > 0 && $nums[$i] === $nums[$i - 1]) {
8+
$ans = [];
9+
$n = count($nums);
10+
for ($i = 0; $i < $n - 2 && $nums[$i] <= 0; ++$i) {
11+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
1512
continue;
1613
}
17-
18-
$left = $i + 1;
19-
$right = $n - 1;
20-
21-
while ($left < $right) {
22-
$sum = $nums[$i] + $nums[$left] + $nums[$right];
23-
24-
if ($sum === 0) {
25-
$triplet = array($nums[$i], $nums[$left], $nums[$right]);
26-
$result[] = $triplet;
27-
28-
while ($left < $right && $nums[$left] === $nums[$left + 1]) {
29-
$left++;
14+
$j = $i + 1;
15+
$k = $n - 1;
16+
while ($j < $k) {
17+
$x = $nums[$i] + $nums[$j] + $nums[$k];
18+
if ($x < 0) {
19+
++$j;
20+
} elseif ($x > 0) {
21+
--$k;
22+
} else {
23+
$ans[] = [$nums[$i], $nums[$j++], $nums[$k--]];
24+
while ($j < $k && $nums[$j] == $nums[$j - 1]) {
25+
++$j;
3026
}
31-
32-
while ($left < $right && $nums[$right] === $nums[$right - 1]) {
33-
$right--;
27+
while ($j < $k && $nums[$k] == $nums[$k + 1]) {
28+
--$k;
3429
}
35-
36-
$left++;
37-
$right--;
38-
} elseif ($sum < 0) {
39-
$left++;
40-
} else {
41-
$right--;
4230
}
4331
}
4432
}
45-
46-
return $result;
33+
return $ans;
4734
}
48-
}
35+
}

0 commit comments

Comments
 (0)