【LeetCode】两数之和

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        dict = {}
        j=0
        for i in nums:
            if (target - i) in dict.values():
                return [nums.index(target - i),j]
            else:
                dict[j] = i
            j += 1

先记录下,回头分析

猜你喜欢

转载自www.cnblogs.com/dreamyu/p/8928798.html