哈希表 主要有记录数值,可以快速查找,效率高
two sums:
1.用一个哈希表记录key 为对应的找的数字,value 为角标
2.通过哈希表来找对应的数字的 角标
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
records = dict()
for index, value in enumerate(nums):
if target - value in records: # 遍历当前元素,并在map中寻找是否有匹配的key
return [records[target- value], index]
records[value] = index # 遍历当前元素,并在map中寻找是否有匹配的key
return []