-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum.py
More file actions
27 lines (21 loc) · 744 Bytes
/
Copy pathtwo_sum.py
File metadata and controls
27 lines (21 loc) · 744 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
'''
Two Sum:
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
'''
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
# create a dict to store num:i
num_to_index = {}
for i, num in enumerate(nums):
if target - num in num_to_index:
return [num_to_index[target - num], i]
else:
num_to_index[num] = i
return []
a = Solution()
x = a.twoSum([1, 2, 5, 6, 9], 8)
print(x)
x = a.twoSum([3, 3, 6, 9], 6)
print(x)