Python easy to understand, consider target is twice of one item situation


  • 0
    R
    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            dict1={}
            for i,v in enumerate(nums):
                dict1[v]=i
            for j in range(len(nums)):
                t=target-nums[j]
                if t in dict1 and dict1[t]!=j:
                    return [dict1[t],j]
    

Log in to reply
 

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