File tree 7 files changed +110
-162
lines changed
0011.Container With Most Water
7 files changed +110
-162
lines changed Original file line number Diff line number Diff line change @@ -151,16 +151,16 @@ impl Solution {
151
151
pub fn max_area (height : Vec <i32 >) -> i32 {
152
152
let mut i = 0 ;
153
153
let mut j = height . len () - 1 ;
154
- let mut res = 0 ;
154
+ let mut ans = 0 ;
155
155
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 ));
157
157
if height [i ] <= height [j ] {
158
158
i += 1 ;
159
159
} else {
160
160
j -= 1 ;
161
161
}
162
162
}
163
- res
163
+ ans
164
164
}
165
165
}
166
166
```
@@ -209,28 +209,23 @@ public class Solution {
209
209
``` php
210
210
class Solution {
211
211
/**
212
- * @param int [] $height
213
- * @return int
212
+ * @param Integer [] $height
213
+ * @return Integer
214
214
*/
215
-
216
215
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;
228
224
} else {
229
- $right-- ;
225
+ --$j ;
230
226
}
231
227
}
232
-
233
- return $maxArea;
228
+ return $ans;
234
229
}
235
230
}
236
231
```
Original file line number Diff line number Diff line change @@ -146,16 +146,16 @@ impl Solution {
146
146
pub fn max_area (height : Vec <i32 >) -> i32 {
147
147
let mut i = 0 ;
148
148
let mut j = height . len () - 1 ;
149
- let mut res = 0 ;
149
+ let mut ans = 0 ;
150
150
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 ));
152
152
if height [i ] <= height [j ] {
153
153
i += 1 ;
154
154
} else {
155
155
j -= 1 ;
156
156
}
157
157
}
158
- res
158
+ ans
159
159
}
160
160
}
161
161
```
@@ -204,28 +204,23 @@ public class Solution {
204
204
``` php
205
205
class Solution {
206
206
/**
207
- * @param int [] $height
208
- * @return int
207
+ * @param Integer [] $height
208
+ * @return Integer
209
209
*/
210
-
211
210
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;
223
219
} else {
224
- $right-- ;
220
+ --$j ;
225
221
}
226
222
}
227
-
228
- return $maxArea;
223
+ return $ans;
229
224
}
230
225
}
231
226
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
/**
3
- * @param int [] $height
4
- * @return int
3
+ * @param Integer [] $height
4
+ * @return Integer
5
5
*/
6
-
7
6
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 ;
19
15
} else {
20
- $right -- ;
16
+ -- $j ;
21
17
}
22
18
}
23
-
24
- return $maxArea ;
19
+ return $ans ;
25
20
}
26
- }
21
+ }
Original file line number Diff line number Diff line change @@ -2,15 +2,15 @@ impl Solution {
2
2
pub fn max_area ( height : Vec < i32 > ) -> i32 {
3
3
let mut i = 0 ;
4
4
let mut j = height. len ( ) - 1 ;
5
- let mut res = 0 ;
5
+ let mut ans = 0 ;
6
6
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 ) ) ;
8
8
if height[ i] <= height[ j] {
9
9
i += 1 ;
10
10
} else {
11
11
j -= 1 ;
12
12
}
13
13
}
14
- res
14
+ ans
15
15
}
16
16
}
Original file line number Diff line number Diff line change @@ -386,49 +386,37 @@ end
386
386
``` php
387
387
class Solution {
388
388
/**
389
- * @param int [] $nums
390
- * @return int [][];
389
+ * @param Integer [] $nums
390
+ * @return Integer [][]
391
391
*/
392
-
393
392
function threeSum($nums) {
394
- $result = [];
395
- $n = count($nums);
396
-
397
393
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]) {
400
398
continue;
401
399
}
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;
415
412
}
416
-
417
- while ($left < $right && $nums[$right] === $nums[$right - 1]) {
418
- $right--;
413
+ while ($j < $k && $nums[$k] == $nums[$k + 1]) {
414
+ --$k;
419
415
}
420
-
421
- $left++;
422
- $right--;
423
- } elseif ($sum < 0) {
424
- $left++;
425
- } else {
426
- $right--;
427
416
}
428
417
}
429
418
}
430
-
431
- return $result;
419
+ return $ans;
432
420
}
433
421
}
434
422
```
Original file line number Diff line number Diff line change @@ -378,49 +378,37 @@ end
378
378
``` php
379
379
class Solution {
380
380
/**
381
- * @param int [] $nums
382
- * @return int [][];
381
+ * @param Integer [] $nums
382
+ * @return Integer [][]
383
383
*/
384
-
385
384
function threeSum($nums) {
386
- $result = [];
387
- $n = count($nums);
388
-
389
385
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]) {
392
390
continue;
393
391
}
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;
407
404
}
408
-
409
- while ($left < $right && $nums[$right] === $nums[$right - 1]) {
410
- $right--;
405
+ while ($j < $k && $nums[$k] == $nums[$k + 1]) {
406
+ --$k;
411
407
}
412
-
413
- $left++;
414
- $right--;
415
- } elseif ($sum < 0) {
416
- $left++;
417
- } else {
418
- $right--;
419
408
}
420
409
}
421
410
}
422
-
423
- return $result;
411
+ return $ans;
424
412
}
425
413
}
426
414
```
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
/**
3
- * @param int [] $nums
4
- * @return int [][];
3
+ * @param Integer [] $nums
4
+ * @return Integer [][]
5
5
*/
6
-
7
6
function threeSum ($nums ) {
8
- $result = [];
9
- $n = count ($nums );
10
-
11
7
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 ]) {
15
12
continue ;
16
13
}
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 ;
30
26
}
31
-
32
- while ($left < $right && $nums [$right ] === $nums [$right - 1 ]) {
33
- $right -- ;
27
+ while ($j < $k && $nums [$k ] == $nums [$k + 1 ]) {
28
+ -- $k ;
34
29
}
35
-
36
- $left ++ ;
37
- $right -- ;
38
- } elseif ($sum < 0 ) {
39
- $left ++ ;
40
- } else {
41
- $right -- ;
42
30
}
43
31
}
44
32
}
45
-
46
- return $result ;
33
+ return $ans ;
47
34
}
48
- }
35
+ }
You can’t perform that action at this time.
0 commit comments