-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path16.py
38 lines (33 loc) · 914 Bytes
/
16.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n = len(nums)
if len(nums)==0:
return 0
if len(nums)<3:
sum(nums)
if len(nums)==3:
return sum(nums)
ans = {}
nums.sort()
diff = 999999
for i in range(n):
start = i+1
end = n-1
#t = target-nums[i]
while(start<n and end>=0 and start<end):
s = nums[i]+ nums[start]+ nums[end]
if abs(target-diff)>abs(s-target):
diff = s
if s < target:
start+=1
else:
end-=1
return diff
if __name__ == "__main__":
s = Solution()
print(s.threeSumClosest( [-1, 2, 1, -4], 1))