-
Notifications
You must be signed in to change notification settings - Fork 0
/
threeSumClosest.py
33 lines (32 loc) · 1.09 KB
/
threeSumClosest.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
class Solution(object):
def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort()
minM = 0
closest = target
for n in range(0, len(nums) - 2):
if n != 0 and nums[n] == nums[n-1]: # 如果n重复直接推进
continue
si, i = n + 1, n + 1
sj, j = len(nums) - 1, len(nums) - 1
while i < j:
s = nums[n] + nums[i] + nums[j]
if s == target:
return target
elif s > target or (j != sj and nums[j] == nums[j + 1]):
m = s - target
if minM == 0 or m < minM:
minM = m
closest = s
j -= 1
elif s < target or (i != si and nums[i] == nums[i - 1]):
m = target - s
if minM == 0 or m < minM:
minM = m
closest = s
i += 1
return closest