提交 2d4e4314 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:两数之和

上级 66027e26
from typing import List
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
"""
两数之和
:param nums: 提供的数组
:param target: 目标值
:return: 返回值
"""
hashmap = {} # 字典不可以重复
for index, num in enumerate(nums): # enumerate迭代返回index和value
hashmap[num] = index # 解决value重复的问题
print(hashmap)
for i, num in enumerate(nums):
j = hashmap.get(target - num)
if j is not None and i != j:
return [i, j]
if __name__ == '__main__':
s = Solution()
li = s.twoSum([2, 7, 11, 15], 9)
print(li)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册