Skip to content

Commit 25eb16c

Browse files
committed
Sync LeetCode submission Runtime - 4 ms (49.84%), Memory - 18.8 MB (8.03%)
1 parent e18ea7f commit 25eb16c

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed
Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
# Approach 1 - Two Pointers
1+
# Approach 1: Two Pointers
22

3-
# Time: O(N)
3+
# Time: O(n)
44
# Space: O(1)
55

66
class Solution:
77
def twoSum(self, numbers: List[int], target: int) -> List[int]:
8-
n = len(numbers)
9-
left, right = 0, n - 1
10-
11-
while left < right:
12-
sum_ = numbers[left] + numbers[right]
13-
14-
if sum_ == target:
15-
return [left+1, right+1]
16-
elif sum_ < target:
17-
left += 1
8+
low, high = 0, len(numbers) - 1
9+
10+
while low < high:
11+
total = numbers[low] + numbers[high]
12+
if total == target:
13+
return [low + 1, high + 1]
14+
elif total < target:
15+
low += 1
1816
else:
19-
right -= 1
20-
17+
high -= 1
18+
2119
return [-1, -1]
2220

0 commit comments

Comments
 (0)