← Back

3Sum

Fix one number, then run the converging scan on the rest — plus dedup.

Medium LeetCode ↗ 1 / 1
func threeSum(nums []int) [][]int {

}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

        // Skip duplicate anchors so triplets stay unique.
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
        }
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
            switch {
            case sum < 0:
                left++
            case sum > 0:
                right--
            }
        }
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
            switch {
            case sum < 0:
                left++
            case sum > 0:
                right--
                // Skip duplicate pair values.
            }
        }
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
            switch {
            case sum < 0:
                left++
            case sum > 0:
                right--
            default:
                res = append(res, []int{nums[i], nums[left], nums[right]})
                left++
                right--
                // Skip duplicate pair values.
                for left < right && nums[left] == nums[left-1] {
                    left++
                }
                for left < right && nums[right] == nums[right+1] {
                    right--
                }
            }
        }
    }
}
func threeSum(nums []int) [][]int {
    // Micropattern: sort, fix nums[i], then two-pointer the remainder.
    sort.Ints(nums)
    res := [][]int{}

    for i := 0; i < len(nums)-2; i++ {
        // Skip duplicate anchors so triplets stay unique.
        if i > 0 && nums[i] == nums[i-1] {
            continue
        }

        // Converging scan looking for -nums[i].
        left, right := i+1, len(nums)-1
        for left < right {
            sum := nums[i] + nums[left] + nums[right]
            switch {
            case sum < 0:
                left++
            case sum > 0:
                right--
            default:
                res = append(res, []int{nums[i], nums[left], nums[right]})
                left++
                right--
                // Skip duplicate pair values.
                for left < right && nums[left] == nums[left-1] {
                    left++
                }
                for left < right && nums[right] == nums[right+1] {
                    right--
                }
            }
        }
    }

    return res // O(n^2)
}

Tap the left half to go back, the right half to go forward.

Arrow keys or space to navigate · Esc to exit.