-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.two-sum.py
36 lines (32 loc) · 989 Bytes
/
1.two-sum.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
33
34
35
#
# @lc app=leetcode id=1 lang=python3
#
# [1] Two Sum
#
# @lc code=start
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
# BRUTE FORCE SOLUTION O(n^2)
for i in range(len(nums)):
for j in range(i+1, len(nums)):
sum = nums[i] + nums[j]
if sum == target:
return [i, j]
# BETTER SOLUTION
hash_map = {}
for i in range(len(nums)):
hash_map[nums[i]] = i
for i in range(len(nums)):
search_value = target-nums[i]
if search_value in hash_map and hash_map[search_value] != i:
return [i, hash_map[search_value]]
"""
# BEST SOLUTION
hash_map = {}
for i in range(len(nums)):
search_value = target-nums[i]
if search_value in hash_map:
return[hash_map[search_value], i]
hash_map[nums[i]] = i
# @lc code=end