top of page
Writer's pictureRhash Algo

1. Two Sum

Description:

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.


Examples:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Input: nums = [3,3], target = 6
Output: [0,1]
Input: nums = [3,2,4], target = 6
Output: [1,2]

Solution 1:



class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        length = len(nums)
        for i in range(length):
            for j in range(i + 1, length):
                if nums[i] + nums[j] == target:
                    return [i, j]

Time Complexity: O(n^2)


Solution 2:



class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hash_vals = {}
        for index, num in enumerate(nums):
            if num in hash_vals:
                hash_vals[num].append(index)
            else:
                hash_vals[num] = [index]
        for index, num in enumerate(nums):
            second = target - num
            if second in hash_vals:
                that_val = hash_vals[second]
                if second == num:
                    if len(that_val) > 1:
                        return that_val
                    else:
                        continue
                else:
                    return [index, that_val[0]]

Time Complexity: O(n)







4 views0 comments

Comments


bottom of page