-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.py
More file actions
24 lines (22 loc) · 851 Bytes
/
TwoSum.py
File metadata and controls
24 lines (22 loc) · 851 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
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
#if there are two same values in nums, the last one will cover the first one,
#in the for loop, as iterate from the begin to the end, we can fetch both of these two index
d = {value:index for index,value in enumerate(nums)}
index1 = index2 = 1
for i,value in enumerate(nums):
if target - value in d:
index1 += i
index2 += d[target-value]
if target - value == value and index1 == index2:
continue
else:
break
if index1 > index2:
index1, index2 = index2, index1
return [index1,index2]