看家本领不能丢,准备2021年每天一道leetcode题目,尽量不间隔,Python3.8
def two(nums, target):
hasmap = {}
for i, num in enumerate(nums):
if hasmap.get(target - num) is not None:
return [hasmap.get(target - num), i]
hasmap[num] = i
a = [1, 3, 4, 5, 11, 15]
b = 9
c = two(a, b)
print(c)
思路:num2
可以在 num1
之前的 dict
中查找,先检查num2是否存在,然后再去存储哈希表,if和hasmap的顺序一定不要弄混,否则则会出错。