You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Solution:
def twoSum(self, nums: list[int], target: int) -> list[int]:
hash_map = {}
for i, num in enumerate(nums):
complement = target - num # 计算目标值需要的差值
if complement in hash_map: # 查找哈希表中是否存在这个差值
return [hash_map[complement], i] # 找到匹配项,返回两个索引
hash_map[num] = i # 将当前数字和索引存入哈希表
The text was updated successfully, but these errors were encountered:
The text was updated successfully, but these errors were encountered: