-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTwo_Sum.py
More file actions
20 lines (16 loc) · 747 Bytes
/
Two_Sum.py
File metadata and controls
20 lines (16 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Question: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
# Solution: Keep a hashmap with keys of target - num
# Difficulty: Easy
# Time Complexity: O(n)
# Space Complexity: O(n)
from typing import List
def twoSum(nums: List[int], target: int) -> List[int]:
# Create a hashmap to store numbers we want to look for
lookingFor = {}
for i, v in enumerate(nums):
# If the number we're at is one we're looking for
# return it's index along with the one in the hashmap
if v in lookingFor:
return [lookingFor[v], i]
# Otherwise we want to remember to look for the (target - current) later
lookingFor[target - v] = i