** 如何打印返回index1,index2?我尝试了不同的方法但没有打印出来。 **
class Solution:
def twoSum(self, nums, target):
nums = [2,7,11,15]
target = 9
hash_map = {}
for index, value in enumerate(nums):
hash_map[value] = index
for index1, value in enumerate(nums):
if target - value in hash_map:
index2 = hash_map[target - value]
if index1 != index2:
return [index1,index2]
答案 0 :(得分:1)
class Solution:
def twoSum(self, nums, target):
nums = [2,7,11,15]
target = 9
hash_map = {}
for index, value in enumerate(nums):
hash_map[value] = index
for index1, value in enumerate(nums):
if target - value in hash_map:
index2 = hash_map[target - value]
if index1 != index2:
return [index1,index2]
if __name__ == '__main__':
print(Solution().twoSum(9, [2,7,11,15]))
我认为您正在寻找的是main
功能,您可以在其中使用您的功能