func threeSum(nums []int) [][]int {
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
// Too small: move left up to grow the sum.
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
}
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
// Too big: move right down to shrink the sum.
}
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
// Too big: move right down to shrink the sum.
case sum > 0:
right--
}
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
// Too big: move right down to shrink the sum.
case sum > 0:
right--
// Exactly zero: record the triplet, step both pointers inward, then
// skip any repeated values so each triplet is reported only once.
}
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
// Too big: move right down to shrink the sum.
case sum > 0:
right--
// Exactly zero: record the triplet, step both pointers inward, then
// skip any repeated values so each triplet is reported only once.
default:
res = append(res, []int{nums[i], nums[left], nums[right]})
left++
right--
for left < right && nums[left] == nums[left-1] {
left++
}
for left < right && nums[right] == nums[right+1] {
right--
}
}
}
}
}func threeSum(nums []int) [][]int {
// Sorting unlocks two things: the converging scan, and an easy way to skip
// duplicates so we don't report the same triplet twice.
sort.Ints(nums)
res := [][]int{}
// Fix the first number; now we just need two more summing to -nums[i].
for i := 0; i < len(nums)-2; i++ {
// A repeated anchor would only reproduce triplets we already found.
if i > 0 && nums[i] == nums[i-1] {
continue
}
// Converging scan over everything to the right of the anchor.
left, right := i+1, len(nums)-1
for left < right {
sum := nums[i] + nums[left] + nums[right]
switch {
// Too small: move left up to grow the sum.
case sum < 0:
left++
// Too big: move right down to shrink the sum.
case sum > 0:
right--
// Exactly zero: record the triplet, step both pointers inward, then
// skip any repeated values so each triplet is reported only once.
default:
res = append(res, []int{nums[i], nums[left], nums[right]})
left++
right--
for left < right && nums[left] == nums[left-1] {
left++
}
for left < right && nums[right] == nums[right+1] {
right--
}
}
}
}
return res // O(n^2): one anchor loop × one linear scan
}Tap the left half to go back, the right half to go forward.
Arrow keys or space to navigate · Esc to exit.