-
Notifications
You must be signed in to change notification settings - Fork 1
/
_016_3SumClosest.py
37 lines (32 loc) · 1.15 KB
/
_016_3SumClosest.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
#-----------------------------------------------------------------------------
# Runtime: 92ms
# Memory Usage:
# Link:
#-----------------------------------------------------------------------------
class Solution:
def threeSumClosest(self, nums, target: int) -> int:
nums.sort()
result = 0
difference = 10000
for i in range(len(nums)):
if i > 0 and nums[i] == nums[i-1]:
continue
j = i + 1
k = len(nums) - 1
while j < k:
sum_value = nums[i] + nums[j] + nums[k]
new_difference = sum_value - target
if abs(new_difference) < difference:
result = sum_value
difference = abs(new_difference)
if new_difference > 0:
k -= 1
while j < k and nums[k] == nums[k + 1]:
k -= 1
elif new_difference < 0:
j += 1
while j < k and nums[j] == nums[j - 1]:
j += 1
else:
return target
return result