力扣刷题-哈希表-四数之和

本文介绍了如何使用双指针技巧解决四数之和问题,通过排序、去重和调整双指针范围来寻找满足nums[i]+nums[j]+nums[left]+nums[right]=target的不重复四元组,优化了时间复杂度。
摘要由CSDN通过智能技术生成

18 四数之和

题意:给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == target

你可以按 任意顺序 返回答案

思路

四数之和,和15.三数之和 (opens new window)是一个思路,都是使用双指针法, 基本解法就是在15.三数之和 (opens new window)的基础上再套一层for循环。
但是有一些细节需要注意,例如: 不要判断nums[k] > target 就返回了,三数之和 可以通过 nums[i] > 0 就返回了,因为 0 已经是确定的数了,四数之和这道题目 target是任意值。比如:数组是[-4, -3, -2, -1],target是-10,不能因为-4 > -10而跳过。但是我们依旧可以去做剪枝,逻辑变成 nums[i] > target and nums[i] >=0 and target >= 0就可以了。
15.三数之和 (opens new window)的双指针解法是一层for循环num[i]为确定值,然后循环内有left和right下标作为双指针,找到nums[i] + nums[left] + nums[right] == 0。
四数之和的双指针解法是两层for循环nums[k] + nums[i]为确定值,依然是循环内有left和right下标作为双指针,找出nums[k] + nums[i] + nums[left] + nums[right] == target的情况,三数之和的时间复杂度是O(n2),四数之和的时间复杂度是O(n3) 。
那么一样的道理,五数之和、六数之和等等都采用这种解法。
双指针的妙处——>降低复杂度:
对于15.三数之和 (opens new window)双指针法就是将原本暴力O(n3)的解法,降为O(n2)的解法,四数之和的双指针解法就是将原本暴力O(n4)的解法,降为O(n3)的解法。

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        # 同样也是采用双指针法 注意去重 题目说了 a、b、c 和 d 互不相同
        result = [] # 存储结果
        nums = sorted(nums) # 先对nums排序
        for i in range(len(nums)):
            if nums[i] > target and nums[i] >= 0 and target >= 0: # 注意判断条件 题目说了 0 <= a, b, c, d < n
                break 
            if i > 0 and nums[i] == nums[i-1]: # 对a去重
                continue
            for j in range(i+1, len(nums)):
                if nums[i]+nums[j] > target and target >= 0:
                    break
                if j > i+1 and nums[j] == nums[j-1]: # j>i+1
                    continue
                left = j + 1
                right = len(nums)-1
                while right > left:
                    sum_ = nums[i] + nums[j] + nums[left] + nums[right]
                    if sum_ > target:
                        right -= 1
                    elif sum_ < target:
                        left += 1
                    else:
                        result.append([nums[i], nums[j], nums[left], nums[right]])
                        while right > left and nums[left]==nums[left+1]:
                            left += 1
                        while right > left and nums[right]==nums[right-1]:
                            right -= 1
                        # 无论如何都需要移动left和right
                        left += 1
                        right -= 1
        return result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值