【Leetcode】18. 四数之和
题目链接
代码
go
func fourSum(nums []int, target int) [][]int {
sort.Ints(nums)
ans := make([][]int, 0)
n := len(nums)
for i := 0; i < n-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i+1; j < n-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue
}
l, r := j+1, n-1
for l < r {
sum := nums[i] + nums[j] + nums[l] + nums[r]
if sum == target {
ans = append(ans, []int{nums[i], nums[j], nums[l], nums[r]})
for l < r && nums[l] == nums[l+1] {
l++
}
for l < r && nums[r] == nums[r-1] {
r--
}
l++
r--
}else if sum < target {
l++
}else if sum > target {
r--
}
}
}
}
return ans
}