two sum o(n) solution

PHOTO EMBED

Tue Mar 14 2023 11:01:59 GMT+0000 (Coordinated Universal Time)

Saved by @samuelcodes

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dic = {} #initialise an empty dictionary

        for i, val in enumerate(nums):
            if target - val in dic:
                return [dic[target - val], i]
            else:
                dic[val] = i
content_copyCOPY