-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path16.py
More file actions
25 lines (25 loc) · 735 Bytes
/
Copy path16.py
File metadata and controls
25 lines (25 loc) · 735 Bytes
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
class Solution:
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
ret, minAbs = 0, sys.maxsize
for i in range(len(nums)-2):
j, k = i + 1, len(nums) - 1
minus = target - nums[i]
while j < k:
sum = nums[j] + nums[k]
abso = abs(minus - sum)
if minAbs > abso:
minAbs = abso
ret = sum + nums[i]
if minAbs == 0:
return ret
if sum > minus:
k -= 1
else:
j += 1
return ret