-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path167.py
More file actions
26 lines (25 loc) · 729 Bytes
/
167.py
File metadata and controls
26 lines (25 loc) · 729 Bytes
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
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
leng = len(numbers)
i, j = 0, leng - 1
while i < j:
tmp = numbers[i] + numbers[j]
if tmp == target:
break
elif tmp > target:
j -= 1
else:
i += 1
return i+1, j+1
# dictionary. Another solution copied from others
def twoSum2(self, numbers, target):
dic = {}
for i, num in enumerate(numbers):
if target - num in dic:
return [dic[target - num] + 1, i + 1]
dic[num] = i