-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo16 3Sum Closest.py
32 lines (31 loc) · 949 Bytes
/
No16 3Sum Closest.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
class Solution:
def threeSumClosest(self, nums, target):
closest = float('inf')
res = 0
if not nums:
return []
n = len(nums)
nums.sort()
for i in range(n):
j = i+1
k = n-1
while j < k:
m = nums[i] + nums[j] + nums[k]
print(m)
if m == target:
return m
elif m < target:
if abs(target - m) < closest:
closest = abs(target - m)
res = m
j += 1
else:
if abs(target - m) < closest:
closest = abs(target - m)
res = m
print(11111111111)
k -= 1
return res
if __name__ == '__main__':
Solu = Solution()
print(Solu.threeSumClosest([1,1,1,1], 0))