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 0492572 commit dcfe111Copy full SHA for dcfe111
14. Questions/leetcode 167 - two sum II.py
@@ -0,0 +1,21 @@
1
+# two sum II - input array is sorted | leetcode 167 | https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
2
+# use two pointers on sorted array; if sum > target slide window left, else slide window right
3
+
4
+class Solution:
5
+ def twoSum(self, numbers: list[int], target: int) -> list[int]:
6
+ ptrL = 0
7
+ ptrR = 1
8
+ N = len(numbers)
9
10
+ while ptrR < N:
11
+ s = numbers[ptrR] + numbers[ptrL]
12
+ if s == target:
13
+ return [ptrL + 1, ptrR + 1]
14
+ elif s < target:
15
+ ptrL += 1
16
+ ptrR += 1
17
+ else:
18
+ ptrL -= 1
19
20
+ # unreachable for testcases with exactly one solution
21
+ return [-1, -1]
0 commit comments