From b32a66c2f8d13ea6a7305fa08b891cc607ba9765 Mon Sep 17 00:00:00 2001 From: sainaga00 Date: Wed, 18 Feb 2026 18:49:58 -0500 Subject: [PATCH] CC2 --- twoSum.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 twoSum.py diff --git a/twoSum.py b/twoSum.py new file mode 100644 index 00000000..4b89e90f --- /dev/null +++ b/twoSum.py @@ -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] +