四数之和GO语言实现

视频讲解:难在去重和剪枝!| LeetCode:18. 四数之和_哔哩哔哩_bilibili

文章讲解:代码随想录

力扣地址:. - 力扣(LeetCode)

记录:比三数之和多加一个循环

func fourSum(nums []int, target int) (ans [][]int) {
    sort.Ints(nums)
    n := len(nums)
    for a := 0; a < n-3; a++ { // 枚举第一个数
        x := nums[a]
        if a > 0 && x == nums[a-1] { // 跳过重复数字
            continue
        }
        if x+nums[a+1]+nums[a+2]+nums[a+3] > target { // 优化一
            break
        }
        if x+nums[n-3]+nums[n-2]+nums[n-1] < target { // 优化二
            continue
        }
        for b := a + 1; b < n-2; b++ { // 枚举第二个数
            y := nums[b]
            if b > a+1 && y == nums[b-1] { // 跳过重复数字
                continue
            }
            if x+y+nums[b+1]+nums[b+2] > target { // 优化一
                break
            }
            if x+y+nums[n-2]+nums[n-1] < target { // 优化二
                continue
            }
            c, d := b+1, n-1
            for c < d { // 双指针枚举第三个数和第四个数
                s := x + y + nums[c] + nums[d] // 四数之和
                if s > target {
                    d--
                } else if s < target {
                    c++
                } else {
                    ans = append(ans, []int{x, y, nums[c], nums[d]})
                    for c++; c < d && nums[c] == nums[c-1]; c++ {} // 跳过重复数字
                    for d--; d > c && nums[d] == nums[d+1]; d-- {} // 跳过重复数字
                }
            }
        }
    }
    return
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值