Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions twoSum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Time Complexity : O(n)
# Space Complexity : O(n)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach: Use a hashmap to store (number : index) for numbers seen so far.
# For each nums[i], check if its complement (target - nums[i]) already exists in the hashmap.
# If yes, return the stored index and current index, otherwise store nums[i] with its index.


class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
h_map = {}
for i in range(len(nums)):
if target - nums[i] in h_map:
return [h_map[target - nums[i]], i]
h_map[nums[i]] = i

return [-1, -1]