classSolution(object): defthreeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ rint = 9999999 rint1 = 0 for i in range(len(nums)): for j in range(i+1, len(nums)): for k in range(j+1, len(nums)): a = nums[i] + nums[j] + nums[k] - target if a >= 0and a <= rint: rint = a rint1 = a + target elif a < 0and -a <= rint: rint = -a rint1 = a + target return rint1
classSolution(object): defthreeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ rint = 9999999 rint1 = 0 nums.sort() for i in range(len(nums)): if i == 0or nums[i] > nums[i-1]: if nums[i] - rint > target and nums[i] > 0: break for j in range(i+1, len(nums)): if j == i+1or nums[j] > nums[j-1]: if nums[i] + nums[j] - rint > target and nums[j] > 0: break for k in range(j+1, len(nums)): if k == j+1or nums[k] > nums[k-1]: if nums[i] + nums[j] + nums[k] - rint > target and nums[k] > 0: break a = nums[i] + nums[j] + nums[k] - target if a >= 0and a <= rint: rint = a rint1 = a + target elif a < 0and -a <= rint: rint = -a rint1 = a + target return rint1
classSolution(object): defthreeSumClosest(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ rint = 9999999 rint1 = 0 nums.sort() for i in range(len(nums)): if i == 0or nums[i] > nums[i-1]: l = i + 1 r = len(nums) - 1 while l < r: s = nums[i] + nums[l] + nums[r] a = s - target if a < 0: l += 1 if -a < rint: rint = -a rint1 = s elif a > 0: r -= 1 if a < rint: rint = a rint1 = s else: rint = 0 rint1 = target break; return rint1