func fourSum(nums []int, target int) [][]int {
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
sort.Ints(nums)
n := len(nums)
res := [][]int{}
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
sort.Ints(nums)
n := len(nums)
res := [][]int{}
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue // skip duplicate first anchor
}
}
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
sort.Ints(nums)
n := len(nums)
res := [][]int{}
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue // skip duplicate first anchor
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue // skip duplicate second anchor
}
}
}
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
sort.Ints(nums)
n := len(nums)
res := [][]int{}
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue // skip duplicate first anchor
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue // skip duplicate second anchor
}
left, right := j+1, n-1
for left < right {
sum := nums[i] + nums[j] + nums[left] + nums[right]
switch {
case sum < target:
left++
case sum > target:
right--
}
}
}
}
}
func fourSum(nums []int, target int) [][]int {
// Micropattern: fix TWO anchors (i, j), then two-pointer the rest.
sort.Ints(nums)
n := len(nums)
res := [][]int{}
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue // skip duplicate first anchor
}
for j := i + 1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue // skip duplicate second anchor
}
left, right := j+1, n-1
for left < right {
sum := nums[i] + nums[j] + nums[left] + nums[right]
switch {
case sum < target:
left++
case sum > target:
right--
default:
res = append(res, []int{nums[i], nums[j], 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^3)
}
Tap the left half to go back, the right half to go forward.
Arrow keys or space to navigate · Esc to exit.