【Leetcode】18. 4Sum


问题描述

Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

注意

The solution set must not contain duplicate quadruplets.

例子

Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]


2019-06-05

基本思路与3Sum一致,没多考虑就直接写代码了,但是不凑巧的报错了。

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        L = len(nums)
        nums.sort()
        result = []
        for i in range(L):
            for j in range(i+1,L):
                target_tmp = target - nums[i] - nums[j]
                left,right = j+1 , L-1
                while(left<right):
                    sum_tmp = nums[left] + nums[right]
                    if sum_tmp<target_tmp:
                        left = left + 1
                    elif sum_tmp > target_tmp:
                        right = right - 1
                    else:
                        result.append([nums[i],nums[j],nums[left],nums[right]])
                         while(left<right) and nums[left]== nums[left+1]:
                             left = left + 1
                         while(left<right) and nums[right]== nums[right-1]:
                             right = right - 1
                        left = left + 1
                        right = right - 1
        # print(result)
        return result

S = Solution()
nums = [1, 0, -1, 0, -2, 2]
target = 0
S.fourSum(nums,target)

从报错信息上看,是出现了两个相同解。下面思考该如何去除重复的四元组。
wrong answer

问题出在第二层循环中产生了重复数据,采用如下方式去除。

                if j>i+1 and nums[j]==nums[j-1]:
                    continue

添加上述报错的错误样例之后,产生如下:
wrong answer2
这里发现似乎内层循环同样出现此问题,采用同样的方式解决。

                if j>i+1 and nums[j]==nums[j-1]:
                    continue
结果分析

Runtime: 820 ms, faster than 32.75% of Python online submissions for 4Sum.
Memory Usage: 11.8 MB, less than 45.02% of Python online submissions for 4Sum.
时间空间复杂度都比较低,可以采用以下方式微调。

			#skip duplication
            if i>0 and nums[i] == nums[i-1]:
                continue
            #skip uneccesary case
            if nums[i]*4>target:
                continue

				# skip duplication
                if j>i+1 and nums[j]==nums[j-1]:
                    continue
                # skip uneccesary case
                if nums[j]*3 > target - nums[i]:
                    continue

最后得到的结果如下,可以看出在时间复杂度上还是有相应的提升。

Runtime: 412 ms, faster than 51.73% of Python online submissions for 4Sum.
Memory Usage: 11.9 MB, less than 31.24% of Python online submissions for 4Sum.

但总的说来,先排序、后利用双指针夹逼的时空复杂度都比较高,后面有时间会继续分析。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值