Short O(n) time complexity solution in python


  • 1
    A
    class Solution:
        # @return a tuple, (index1, index2)
        def twoSum(self, num, target):
            map_ = {target - num[0] : 0}
            count = 1
            while count < len(num):
                number = num[count]
                if number in map_:
                    return (map_[number] + 1, count + 1)
                else:
                    map_[target-number] = count
                count = count + 1
            return (-1, -1)

Log in to reply
 

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