-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwo_sum.py
More file actions
39 lines (31 loc) · 1.1 KB
/
Copy pathtwo_sum.py
File metadata and controls
39 lines (31 loc) · 1.1 KB
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
36
37
38
39
"""
Given an array of integers,
return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution,
and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
"""
from typing import List
# noinspection PyPep8Naming,PyMethodMayBeStatic
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
num2index = {}
for index, num in enumerate(nums):
prev = num2index.get(target - num)
if prev is None:
num2index[num] = index
else:
return [prev, index]
raise RuntimeError('No two sum solution!')
# Python 2
# Runtime: 32 ms, faster than 89.77% of Python online submissions for Two Sum.
# Memory Usage: 13.3 MB, less than 17.41% of Python online submissions
# for Two Sum.
#
# Python 3
# Runtime: 36 ms, faster than 93.97% of Python3 online submissions for Two Sum.
# Memory Usage: 14.5 MB, less than 26.27% of Python3 online submissions
# for Two Sum.