Python HashTable Solution


  • 0
    _

    Classic solution to this problem. (But doesn't use the fact that the input is sorted.)

    Still, an intuitive solution, and a good place to start.

    import collections
    class Solution(object):
        def twoSum(self, numbers, target):
            """
            :type numbers: List[int]
            :type target: int
            :rtype: List[int]
            """
            seen = collections.defaultdict(list)
    
            for idx in range(0,len(numbers)):
                value = numbers[idx]
                if seen[value]:
                    seen[value].append(idx)
                    res = seen[value]
                    return map(lambda x: x+1, res)
                    
                seen[target - value].append(idx)
            
            return None
    

Log in to reply
 

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