-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001-two-sum.py
More file actions
27 lines (25 loc) · 827 Bytes
/
001-two-sum.py
File metadata and controls
27 lines (25 loc) · 827 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
27
class Solution_me:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hash_table = {}
count = 0
for i in nums:
hash_table[i] = count
count += 1
count = 0
for i in nums:
try:
if count != hash_table[target-i]:
return [count, hash_table[target-i]]
except:
pass
count += 1
return 0
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num_count = {}
for i in range(len(nums)):
num_count[nums[i]] = i
for i in range(len(nums)):
if target - nums[i] in num_count and \
i != num_count[target - nums[i]]:
return [i, num_count[target - nums[i]]]