-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-TwoSum.py
32 lines (28 loc) · 863 Bytes
/
1-TwoSum.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
32
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# One-pass Hash Table
hash_map = {}
for index in range(len(nums)):
rest = target - nums[index]
if rest in hash_map:
return [index, hash_map[rest]]
hash_map[nums[index]] = index
def twoSum1(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
# Two-pass Hash Table
hash_map = {}
for i in range(len(nums)):
hash_map[nums[i]] = i
for i in range(len(nums)):
rest = target - nums[i]
if rest in hash_map and hash_map[rest] != i:
return [i, hash_map[rest]]