python3刷力扣--两数之和

时间:2021-09-02 08:32:28   收藏:0   阅读:16

https://leetcode-cn.com/problems/two-sum/
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
adict={}
for i in range(len(nums)): # 遍历列表
if target-nums[i] not in adict: # 每遍历一个都记录下值和index,当后续遍历的值满足与已经遍历的值之和为target,则找到了,返回正在遍历的位置和已经遍历过的值的位置即可
adict[nums[i]]=i
else:
return [i,adict[target-nums[i]]]

原文:https://www.cnblogs.com/liuyongbo/p/15207977.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!