fix:两数之和

上级 1396d0c9
"""
两数之和 II - 输入有序数组
"""
import heapq
from typing import List
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
"""
两数之和 使用字典
:param numbers:
:param target:
:return:
"""
dict1 = {}
for i, num in enumerate(numbers):
dict1[num] = i
for j, num in enumerate(numbers):
# 先遍历小的
remain = target - num
remain_ = dict1.get(remain)
if remain_ is not None and j < remain_:
return [j + 1, remain_ + 1]
return []
if __name__ == '__main__':
result = Solution().twoSum([5, 25, 75], 100)
print(result)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册