1. Two Sum

Easy

Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same.

😇 Solution

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

        for i in range(l):
            t = target - nums[i]
            if t in temp.keys() and temp[t] != i:
                ans.append(i)
                ans.append(temp[t])
                return ans

        return ans
        

Last updated

Was this helpful?