2017-09-14 113 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功能,您可以在其中使用功能