from 2022.6.19 From the beginning to the end of the autumn move , I will update at least one question every day leetcode I understand this blog On , Stick it up as evidence .
class Solution:
def twoSum(self, nums, target):
""" :type nums: List[int] :type target: int :rtype: List[int] """
hashmap = {
}
for index, num in enumerate(nums):
num2 = target - num
if num2 in hashmap:
return [hashmap[num2], index]
hashmap[num] = index
return None
enumerate function : You can get the index and element of an iteratable object , Store elements and indexes in a dictionary , Every time calculation target-num, If this value is in the key Note that the sum of two numbers in the list is equal to target, Return two numbers and the subscript in the array .