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 e18ea7f commit 25eb16cCopy full SHA for 25eb16c
0167-two-sum-ii---input-array-is-sorted/solution.py
@@ -1,22 +1,20 @@
1
-# Approach 1 - Two Pointers
+# Approach 1: Two Pointers
2
3
-# Time: O(N)
+# Time: O(n)
4
# Space: O(1)
5
6
class Solution:
7
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
+ low, high = 0, len(numbers) - 1
+
+ while low < high:
+ total = numbers[low] + numbers[high]
+ if total == target:
+ return [low + 1, high + 1]
+ elif total < target:
+ low += 1
18
else:
19
- right -= 1
20
+ high -= 1
21
return [-1, -1]
22
0 commit comments