2017-09-14 111 views
1

** 如何打印返回索引1,索引2?我尝试不同的方式,但没有打印出来。 **如何打印返回值?

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

有哪些不同的方式,你试过吗?编辑你的问题,并包括这些尝试。 –

+0

请编辑您的问题,以包含您尝试过的[最小,**完整**和可验证示例](http://stackoverflow.com/help/mcve)。 –

+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功能,您可以在其中使用功能