Accepted Python O(N) solution


  • 0
    A
    def twoSum(self, nums, target):
            # Create a num to index dictionary
            index_map = {}
            
            for i in xrange(len(nums)):
                if nums[i] in index_map:
                    return [index_map[nums[i]], i]
                
                # create entry
                index_map[target - nums[i]] = i
    

Log in to reply
 

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