My python code only 7 lines, 48 ms.


  • 2
    X
    class Solution:
        # @param {integer[]} nums
        # @param {integer} target
        # @return {integer[]}
        def twoSum(self, nums, target):
            d = {i:idx for idx, i in enumerate(nums)}
            for idx, i in enumerate(nums):
                re = d.get(target-i, -1)
                if re != -1 and re != idx:
                    return [idx+1, re+1]

Log in to reply
 

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