-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0001_Two_Sum.py
29 lines (26 loc) · 917 Bytes
/
0001_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
"""
STATEMENT
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
CLARIFICATIONS
- Each input has exactly 1 solution
- The same element may not be used twice
- The list is not empty
- The list is not necessarily sorted
EXAMPLE:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1]
COMMENTS:
We can traverse the array and record the seen integers using a O(n) complexity set. set()
Iterate the list and at each step, look to see if there's a complement of the current item that exists in the set
O(n) time/space complexity
"""
class Solution(object):
def twoSum(self, nums, target):
s = set()
for i in range(0,len(nums)):
complement = target - nums[i]
if (complement in s):
return [nums.index(complement),i]
s.add(nums[i])
return [0,0]