1.两数之和(Python实现)

1.两数之和(Python实现)_第1张图片
思路:用字典记录下每个元素的位置

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dic={}
        for index,vaule in enumerate(nums):
            sub=target-vaule
            if sub in dic:
                return [dic[sub],index]
            else:
                dic[vaule]=index

你可能感兴趣的:(LeetCode,Python)