We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f4cdf21 commit 0f0492aCopy full SHA for 0f0492a
0016-3sum-closest/solution.py
@@ -1,28 +1,25 @@
1
-# Approach 1 - Two Pointers
+# Approach 1: Two Pointers
2
3
-# Time: O(n^2)
+# Time: O(n ^ 2)
4
5
class Solution:
6
def threeSumClosest(self, nums: List[int], target: int) -> int:
7
diff = float('inf')
8
nums.sort()
9
-
+
10
for i in range(len(nums)):
11
lo, hi = i + 1, len(nums) - 1
12
13
while lo < hi:
14
total = nums[i] + nums[lo] + nums[hi]
15
if abs(target - total) < abs(diff):
16
diff = target - total
17
18
if total < target:
19
lo += 1
20
else:
21
hi -= 1
22
23
- if diff == 0:
24
- break
25
+ if diff == 0:
+ break
26
return target - diff
27
28
0 commit comments