-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum Closest.py
74 lines (69 loc) · 2.31 KB
/
3Sum 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# python3
# medium
# 数组 双指针
# 88ms 95.13%
# 13MB 47.01%
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
res = nums[0] + nums[1] + nums[2]
for i in range(len(nums)-2):
j = i + 1
k = len(nums) - 1
min_sum = nums[i] + nums[j] + nums[j+1]
max_sum = nums[i] + nums[k-1] + nums[k]
if min_sum > target:
if abs(min_sum - target) < abs(res - target):
res = min_sum
break
if max_sum < target:
if abs(max_sum - target) < abs(res - target):
res = max_sum
continue
while j < k:
s = nums[i] + nums[j] + nums[k]
if s == target:
return s
elif abs(s - target) < abs(res - target):
res = s
elif s > target:
k -= 1
while j < k and nums[k + 1] == nums[k]:
k -= 1
else:
j += 1
while j < k and nums[j] == nums[j - 1]:
j += 1
return res
# 144ms 51.14%
# 13.1MB 48.75%
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
nums.sort()
res = nums[0] + nums[1] + nums[2]
for i in range(len(nums)):
j = i + 1
k = len(nums) - 1
while j < k:
s = nums[i] + nums[j] + nums[k]
if s == target:
return s
elif abs(s - target) < abs(res - target):
res = s
if s > target:
k -= 1
while j < k and nums[k + 1] == nums[k]:
k -= 1
else:
j += 1
while j < k and nums[j] == nums[j - 1]:
j += 1
elif s > target:
k -= 1
while j < k and nums[k + 1] == nums[k]:
k -= 1
else:
j += 1
while j < k and nums[j] == nums[j - 1]:
j += 1
return res