-
Notifications
You must be signed in to change notification settings - Fork 0
/
threesumcloest.py
39 lines (32 loc) · 933 Bytes
/
threesumcloest.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
"""
Task:
Given an array S of n integers, find three integers in S such that the sum
is closest to a given number, target. Return the sum of the three integers.
You may assume that each input would have exactly one solution.
>>> three_sum_cloest([-1, 2, 1, -4], target=1)
2
>>> three_sum_cloest([0, 0, 0], 1)
0
"""
def three_sum_cloest(nums, target):
n = len(nums)
if n <= 3:
return sum(nums)
nums = sorted(nums)
ans = nums[0] + nums[1] + nums[2]
for i in range(0, n - 2):
j, k = i + 1, n - 1
while j < k:
cur = nums[i] + nums[j] + nums[k]
if cur == target:
return target
elif cur < target:
j += 1
else:
k -= 1
if abs(cur - target) < abs(ans - target):
ans = cur
return ans
if __name__ == '__main__':
import doctest
doctest.testmod()