0 && $nums[$i] !== $nums[$i - 1])) { [$low, $high, $sum] = [$i + 1, $n - 1, -$nums[$i]]; while ($low < $high) { if ($nums[$low] + $nums[$high] === $sum) { $ans[] = [$nums[$i], $nums[$low], $nums[$high]]; while ($low < $high && $nums[$low] === $nums[$low + 1]) { $low++; } while ($low < $high && $nums[$high] === $nums[$high - 1]) { $high--; } $low++; $high--; } elseif ($nums[$low] + $nums[$high] < $sum) { $low++; } else { $high--; } } } } return $ans; } public static function threeSum3(array $nums): array { [$ans, $n] = [[], count($nums)]; if (empty($nums) || $n < 3) { return $ans; } sort($nums); for ($i = 0; $i < $n - 2; $i++) { if ($i > 0 && $nums[$i] === $nums[$i - 1]) { continue; } $low = $i + 1; $high = $n - 1; while ($low < $high) { $sum = $nums[$i] + $nums[$low] + $nums[$high]; if ($sum > 0) { $high--; } elseif ($sum < 0) { $low++; } else { $ans[] = [$nums[$i], $nums[$low], $nums[$high]]; while ($low < $high && $nums[$low] === $nums[$low + 1]) { $low++; } while ($low < $high && $nums[$high] === $nums[$high - 1]) { $high--; } $low++; $high--; } } } return $ans; } }