My accepted concise Python solution


  • 2
    F

    O(n) time, O(n) space

    class Solution:
        # @return a tuple, (index1, index2)
        def twoSum(self, num, target):
            map = {} # maps element to its index
            for i in range(len(num)):
                if target - num[i] in map:
                    i1 = i + 1
                    i2 = map[target - num[i]] + 1
                    return (min(i1, i2), max(i1, i2))
                else:
                    map[num[i]] = i

Log in to reply
 

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