标签:code NPU pre enter list split sel val 查询
class TwoSum:
def __init__(self,nums,target):
self.nums = nums
self.target = target
def sum(self):
hash_dict = { }
for index, value in enumerate(self.nums):
# 在字典中查询其差值是否在字典中
if hash_dict.get(self.target - value) is not None:
#如果在字典中,通过get方法找到差值的索引
return [hash_dict.get(self.target - value), index]
# 如果差值不在字典中,将其元素作为键,其索引作为值存入字典
hash_dict[value] = index
nums = list(map(int,input("Enter nums:\n").split( )))
target = int(input("Enter target:\n"))
num = TwoSum(nums,target).twosum()
print(num)
Enter nums:
0 2 3
Enter target:
5
[1, 2]
标签:code NPU pre enter list split sel val 查询
原文地址:https://www.cnblogs.com/sinlearn/p/12831457.html