其他分享
首页 > 其他分享> > 查找表_leetcode16

查找表_leetcode16

作者:互联网

#coding=utf-8
# 解题思路: 排序 + 双指针求和 20190302 找工作期间
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
mindiff = 10000
nums.sort()
res = 0

for i in range(len(nums)):
left = i+1
right = len(nums)-1

while left < right:
sum = nums[left] + nums[right] + nums[i]
diff = abs(target -sum )

if diff < mindiff:
mindiff = diff
res = sum

if target == sum :
return sum

elif sum < target:
left += 1

else:
right -= 1

return res

标签:right,target,nums,int,sum,查找,leetcode16,left
来源: https://www.cnblogs.com/lux-ace/p/10546894.html