1、两数之和

1、两数之和

难度:简单
题目:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标

你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。

Python:

创建哈希表

class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        hashmap = {}
        for index, num in enumerate(nums):
            if target - num in hashmap:
                return [hashmap[target - num], index]
        hashmap[num] = index
posted @ 2020-08-20 22:01  ZeroCrow  阅读(65)  评论(0)    收藏  举报