Leetcode-16. 最接近的三数之和

Leetcode-16. 最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数,使得它们的和与 target 最接近。返回这三个数的和。假定每组输入只存在唯一答案。

例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.

与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).

基础思路

和三数之和问题思路类似,控制一个变量,然后用双指针

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        n=len(nums)
        ans=nums[0]+nums[1]+nums[2]
        for i in range(n):
            start=i+1
            end=n-1
            while start<end:
                sum=nums[i]+nums[start]+nums[end]
                if abs(sum-target)<abs(ans-target):
                    ans=sum
                if sum>target:
                    end =end-1
                elif sum<target:
                    start=start+1
                else:
                    return ans
        return ans
        

优化算法

上面算法优化方案最容易想到的就是忽略重复元素,避免重复运算。可见,该算法明显提升了运算速度

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        n=len(nums)
        ans=nums[0]+nums[1]+nums[2]
        for i in range(n):
            if i>0 and nums[i]==nums[i-1]:
                continue
            start=i+1
            end=n-1
            while start<end:
                sum=nums[i]+nums[start]+nums[end]
                if abs(sum-target)<abs(ans-target):
                    ans=sum
                if sum>target:
                    end =end-1
                    while start<end and nums[end]==nums[end+1]:
                        end=end-1
                elif sum<target:
                    start=start+1
                    while start<end and nums[start]==nums[start-1]:
                        start=start+1
                else:
                    return ans
        return ans
        

执行用时 :68 ms, 在所有 python 提交中击败了95.12%的用户

内存消耗 :11.7 MB, 在所有 python 提交中击败了33.21%的用户

进一步优化

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        n=len(nums)
        ans=nums[0]+nums[1]+nums[2]
        for i in range(n):
            if i>0 and nums[i]==nums[i-1]:
                continue
            start=i+1
            end=n-1
            
            while start<end:
                min=nums[i]+nums[start]+nums[start+1]
                if target<min:
                    if(abs(min-target)<abs(ans-target)):
                        ans=min
                    break
                max=nums[i]+nums[end]+nums[end-1]
                if target>max:
                    if(abs(max-target)<abs(ans-target)):
                        ans=max
                    break
                sum=nums[i]+nums[start]+nums[end]
                if abs(sum-target)<abs(ans-target):
                    ans=sum
                if sum>target:
                    end =end-1
                    while start<end and nums[end]==nums[end+1]:
                        end=end-1
                elif sum<target:
                    start=start+1
                    while start<end and nums[start]==nums[start-1]:
                        start=start+1
                else:
                    return ans
        return ans
        

因为nums已经是排序了的,所以在总能在start和end之间一开始就确认最大值最小值。前两个元素就是最小元素,其和一定也是最小值。同理,最后两个元素和为最大值。如果target比min还小,那么离target最近的一定是min值了,不需要在依次移动指针了。如果target比max还大,同理。

执行用时 :44 ms, 在所有 python 提交中击败了98.98%的用户

内存消耗 :11.7 MB, 在所有 python 提交中击败了37.87%的用户

小总结:

优化算法的一条就是在基本算法上,根据具体的题目增加细节化的控制条件,使得在一些特殊情况下,简化运算量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值