-
Notifications
You must be signed in to change notification settings - Fork 0
/
16_three_sum_closest.py
38 lines (34 loc) · 1.1 KB
/
16_three_sum_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
33
34
35
36
37
38
def threeSumClosest(nums, target):
nums.sort()
closest_three_sum = None
for i, num in enumerate(nums):
if i > 0 and num == nums[i - 1]:
continue
l = i + 1
r = len(nums) - 1
while l < r:
sum = num + nums[l] + nums[r]
# print(sum)
if closest_three_sum is None:
# print("hehe")
closest_three_sum = sum
else:
diff_1 = abs(closest_three_sum - target)
diff_2 = abs(sum - target)
# print("diff_1,", diff_1, diff_2)
if diff_1 > diff_2:
closest_three_sum = sum
if sum < target:
l += 1
elif sum > target:
r -= 1
else:
l += 1
while nums[l] == nums[l - 1] and l < r:
l += 1
r -= 1
while nums[r] == nums[r + 1] and l < r:
r -= 1
return closest_three_sum
print(threeSumClosest([-1, 2, 1, -4], 1))
# print(threeSumClosest([0, 0, 0], 1))