LeetCode 16 3Sum Closest (Python)

Posted by 小明MaxMing on November 14, 2024

题目

Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.

Return the sum of the three integers.

You may assume that each input would have exactly one solution.

解题思路

把数组排序,枚举第一个数,之后两个指针枚举后两个数,维护一个与target的差值的最小gap

代码

class Solution:
    def threeSumClosest(self, nums: List[int], target: int) -> int:
        nums.sort()
        gap = float("inf")
        for i in range(len(nums)):
            cur = nums[i]
            j = i + 1
            k = len(nums) -1
            while j < k:
                tmp = nums[i] + nums[j] + nums[k] - target
                gap = tmp if abs(tmp) < abs(gap) else gap
                if tmp > 0:
                    k -= 1
                elif tmp < 0:
                    j += 1
                else:
                    return target
        return target + gap

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅