class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hashmap={} # 定义字典
for i,num in enumerate(nums):
if hashmap.get(target - num) is not None:
return [hashmap.get(target - num), i]
hashmap[num] = i
if __name__ == "__main__":
nums = [2, 9, 7, 6]
target = 8
sou = Solution()
sou.twoSum(nums, target)
list中两数之和等于给定的另一个数 python优化解法
最新推荐文章于 2024-06-17 17:35:06 发布