forked from wuduhren/leetcode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo-sum.py
More file actions
21 lines (19 loc) · 676 Bytes
/
two-sum.py
File metadata and controls
21 lines (19 loc) · 676 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#https://leetcode.com/problems/two-sum/
"""
when we iterate through the nums
we don't know if the counter part exist
so we store the counter part we want in the 'wanted' with the index now
so inside the wanted is {the_counter_part_we_want:index_now} [0]
later on if we found the num is in 'wanted'
we return the index_now and the index_now we previously stored in the 'wanted' [1]
"""
class Solution(object):
def twoSum(self, nums, target):
wanted = {}
for i in xrange(len(nums)):
n = nums[i]
if n in wanted: #[1]
return [wanted[n], i]
else:
wanted[target-n] = i #[0]
return []