File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -310,6 +310,49 @@ var fourSum = function(nums, target) {
310
310
};
311
311
```
312
312
313
+ PHP:
314
+ ``` php
315
+ class Solution {
316
+ /**
317
+ * @param Integer[] $nums
318
+ * @param Integer $target
319
+ * @return Integer[][]
320
+ */
321
+ function fourSum($nums, $target) {
322
+ $res = [];
323
+ sort($nums);
324
+ for ($i = 0; $i < count($nums); $i++) {
325
+ if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
326
+ continue;
327
+ }
328
+ for ($j = $i + 1; $j < count($nums); $j++) {
329
+ if ($j > $i + 1 && $nums[$j] == $nums[$j - 1]) {
330
+ continue;
331
+ }
332
+ $left = $j + 1;
333
+ $right = count($nums) - 1;
334
+ while ($left < $right) {
335
+ $sum = $nums[$i] + $nums[$j] + $nums[$left] + $nums[$right];
336
+ if ($sum < $target) {
337
+ $left++;
338
+ }
339
+ else if ($sum > $target) {
340
+ $right--;
341
+ }
342
+ else {
343
+ $res[] = [$nums[$i], $nums[$j], $nums[$left], $nums[$right]];
344
+ while ($left < $right && $nums[$left] == $nums[$left+1]) $left++;
345
+ while ($left < $right && $nums[$right] == $nums[$right-1]) $right--;
346
+ $left++;
347
+ $right--;
348
+ }
349
+ }
350
+ }
351
+ }
352
+ return $res;
353
+ }
354
+ }
355
+ ```
313
356
314
357
-----------------------
315
358
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
You can’t perform that action at this time.
0 commit comments