forked from SocialfiPanda/Leetcode-Py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum Closest.py
More file actions
19 lines (19 loc) · 779 Bytes
/
3Sum Closest.py
File metadata and controls
19 lines (19 loc) · 779 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def threeSumClosest(self, A, target):
A, result, closest_diff, i = sorted(A), 2147483647, 2147483647, 0
while i < len(A) - 2:
j, k = i + 1, len(A) - 1
while j < k:
diff = A[i] + A[j] + A[k] - target
if diff < 0:
if math.fabs(diff) < math.fabs(closest_diff):
result, closest_diff = A[i] + A[j] + A[k], diff
j += 1
elif diff > 0:
if math.fabs(diff) < math.fabs(closest_diff):
result, closest_diff = A[i] + A[j] + A[k], diff
k -= 1
else:
return target
i += 1
return result