-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1.TwoSum.py
executable file
·51 lines (37 loc) · 992 Bytes
/
1.TwoSum.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 9 18:09:01 2020
@author: nenad
"""
"""
Given an numsay 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.
"""
from collections import defaultdict
class Solution:
def twoSum(self, nums, target: int):
indices = defaultdict(list)
n = len(nums)
# O(n)
for i in range(n):
val = nums[i]
ind = indices.get(target-val, None)
if ind:
return [ind[0], i]
indices[val].append(i)
# Test 1
arr = [2,7,11,15]
s = Solution()
print(s.twoSum(arr, 9))
# Test 2
arr = [3,2,4]
#s = Solution()
print(s.twoSum(arr, 6))
# Test 3
arr = [3,3]
#s = Solution()
print(s.twoSum(arr, 6))
# Test 4
arr = [0,3,-3,4,-1]
print(s.twoSum(arr, -1))