-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython167.py
31 lines (24 loc) · 911 Bytes
/
python167.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
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
# Take benefit of sorted input array
# Two pointers technique
l, r = 0, len(numbers) - 1
diff = 0
while l < r:
s = numbers[l] + numbers[r]
if s > target:
r -= 1
elif s < target:
l += 1
elif s == target:
return [l + 1, r + 1]
# O(N) - Exactly the same solution as of the original Two Sum Problem just be mindful of the return sequence of indices.
'''
mp = {}
for i in range(len(numbers)):
need = target - numbers[i]
if need in mp:
return [mp[need] + 1, i + 1]
else:
mp[numbers[i]] = i
'''