O(n) Python


  • 0
    class Solution(object):
        def twoSum(self, nums, target):
            cache = {}
            for i in xrange(len(nums)):
                if target - nums[i] in cache and cache[target - nums[i]] != i:
                    return [cache[target - nums[i]], i]
                cache[nums[i]] = i
    

Log in to reply
 

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