Skip to content

Commit 4d40b13

Browse files
author
MJ Kang
committed
two sum sol
1 parent 5e4c93e commit 4d40b13

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

contains-duplicate/mandoolala.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ def containsDuplicate(self, nums: List[int]) -> bool:
2020
return True
2121
nums_set.add(num)
2222
return False
23-
'''
23+
'''
24+

two-sum/mandoolala.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
3+
class Solution:
4+
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
# return indices of two numbers such that they add up to target
6+
'''
7+
[Complexity]
8+
Time: O(n)
9+
Space: O(1)
10+
'''
11+
nums_dict = {}
12+
for idx, num in enumerate(nums):
13+
remaining = target - num
14+
if remaining in nums_dict:
15+
return [idx, nums_dict[remaining]]
16+
nums_dict[num] = idx
17+
'''
18+
[Complexity]
19+
Time: O(n^2)
20+
Space: O(1)
21+
22+
for i in range(0,len(nums)-1):
23+
for j in range(i+1, len(nums)):
24+
sum = nums[i] + nums[j]
25+
if sum == target:
26+
return [i, j]
27+
'''

0 commit comments

Comments
 (0)