#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param numbers int整型一维数组
# @param target int整型
# @return int整型一维数组
#
class Solution:
def twoSum(self , numbers: List[int], target: int) -> List[int]:
# 初始化哈希表。
hashmap = {}
for i in range(len(numbers)):
# 当前遍历到的数。
num = numbers[i]
# 需要组成target的另一个数。
another_num = target - num
# 如果另一个数在哈希表中,则返回另一个数的索引和当前遍历到的数的(索引+1)。
if another_num in hashmap:
return [hashmap[another_num], i + 1]
# 否则将当前遍历到的数的(索引+1)存入哈希表。
else:
hashmap[numbers[i]] = i + 1

京公网安备 11010502036488号