1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
buff={}
for i, n in enumerate(nums):
d=target-n
if n in buff:
return [buff[n],i]
else:
buff[d]=i
Time complexity is O(n) with space complexity O(n).