Elegant, pythonic solution

  • 0
    class Solution(object):
        def twoSum(self, nums, target):
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            number_dict = dict()
            for index, value in enumerate(nums):
                if target - value in number_dict:
                    return [number_dict[target-value] , index]
                number_dict[value] = index
            return [-1, -1]

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.