3sum closet

在3sum基础上加了一个比较

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        min_t = 100000
        i = 0
        nums.sort()
        while i < len(nums) - 2:
            j = i + 1
            k = len(nums) - 1
            while j < k:
                value = nums[i] + nums[j] + nums[k] 
                if abs(value - target) < min_t:
                    min_t = abs(value - target)
                    result = value
                if value == target:
                    return target
                if value > target:
                    k -= 1
                else:
                    j += 1
            i += 1
        return result
        ```

你可能感兴趣的:(3sum closet)