LeetCode Two Sum

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dt = {}
        index = 0
        while index < len(nums):
            if not nums[index] in dt:
                dt[nums[index]] = index 
            if target - nums[index] in dt: 
                if dt[target-nums[index]] != index :
                    return [dt[target - nums[index]] , index]
            index = index + 1
        dt = {}
        for index, val in enumerate(nums):
            if nums[index] in dt: 
                return [dt[val] , index]
            else:
                dt[target - val] = index 

你可能感兴趣的:(LeetCode Two Sum)