File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change @@ -393,6 +393,46 @@ function threeSum(array $nums): array
393
393
}
394
394
```
395
395
396
+ PHP:
397
+ ``` php
398
+ class Solution {
399
+ /**
400
+ * @param Integer[] $nums
401
+ * @return Integer[][]
402
+ */
403
+ function threeSum($nums) {
404
+ $res = [];
405
+ sort($nums);
406
+ for ($i = 0; $i < count($nums); $i++) {
407
+ if ($nums[$i] > 0) {
408
+ return $res;
409
+ }
410
+ if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
411
+ continue;
412
+ }
413
+ $left = $i + 1;
414
+ $right = count($nums) - 1;
415
+ while ($left < $right) {
416
+ $sum = $nums[$i] + $nums[$left] + $nums[$right];
417
+ if ($sum < 0) {
418
+ $left++;
419
+ }
420
+ else if ($sum > 0) {
421
+ $right--;
422
+ }
423
+ else {
424
+ $res[] = [$nums[$i], $nums[$left], $nums[$right]];
425
+ while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
426
+ while ($left < $right && $nums[$right] == $nums[$right - 1]) $right--;
427
+ $left++;
428
+ $right--;
429
+ }
430
+ }
431
+ }
432
+ return $res;
433
+ }
434
+ }
435
+ ```
396
436
397
437
398
438
-----------------------
You can’t perform that action at this time.
0 commit comments