3 lines, O(n), Python solution

  • 0
    class Solution(object):
        def twoSum(self, nums, target):
            needs = {target - num: i for i, num in enumerate(nums)}
            return next((i, needs[num]) for i, num in enumerate(nums)
                        if num in needs and needs[num] != i)

Log in to reply

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