【问题标题】:From LeetCode Given an array of integers, return indices of the two numbers such that they add up to a specific target来自 LeetCode 给定一个整数数组,返回两个数字的索引,使得它们相加到一个特定的目标
【发布时间】:2018-03-09 11:44:18
【问题描述】:

这是一个关于 LeetCode 的练习。我得到了一个例外

UnboundLocalError 在第 15 行。

为什么?以及如何解决?

类解决方案(对象): def twoSum(self, nums, target): """ :type nums: 列表[int] :类型目标:int :rtype: 列表[int] """ self.nums = [] self.target = int 对于我在范围内(len(nums)): 对于范围内的 j (i + 1, len(nums)): 如果 nums[i] + nums[j] == 目标: 一个 = [] 返回 a[i, j]

【问题讨论】:

  • 请附上问题陈述或在此类帖子中添加问题链接。

标签: python arrays algorithm sorting


【解决方案1】:

我相信这会奏效:

class Solution:
def twoSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """
    n = len(nums)
    for i in range(n):
        for j in  range(i+1, n):
            if nums[i] + nums[j] == target:
                return [i,j]

【讨论】:

    【解决方案2】:

    也许你可以试试:

    if self.nums[i] + self.nums[j] == target:
    #  ^^^^^          ^^^^^
    

    【讨论】:

      【解决方案3】:

      让我们检查您的代码:

      第一种情况(没有组合匹配目标)

      该方法返回一个尚未定义的值:

      return a[i, j]
      

      a 从未被定义!

      第二种情况(组合匹配目标)

      该方法返回一个已初始化的值:

      a = []
      

      但是,我们从未在索引[i, j] 处设置值,所以这仍然行不通:

      return a[i, j]
      

      解决方案

      当你找到一个等于目标的组合时,返回它们的索引:

      class Solution(object):
          def twoSum(self, nums, target):
              """
              :type nums: List[int]
              :type target: int
              :rtype: List[int]
              """
              self.nums = []
              self.target = int
      
              for i in range(len(nums)):
                  for j in range(i + 1, len(nums)):
                      if nums[i] + nums[j] == target:
                          return i, j
      
      if __name__ == "__main__":
          a = Solution()
          print a.twoSum([1,2,3,4,5], 5)
      

      【讨论】:

        【解决方案4】:

        看看这是否有帮助...返回所有可能的索引:

        def getIndices(tlist, target):
            return [(tlist.index(i), tlist.index(j)) for x,i in enumerate(tlist) for j in tlist[x:] if i!=j and i+j==target]
        

        如何调用:

        getIndices(<your-list>, <your-target>)
        

        例子:

        getIndices([1,2,3,4,5,6,7,8], 10) => [(1, 7), (2, 6), (3, 5)]
        getIndices([1,2,3,4,5,6,7,8], 100) => []
        

        【讨论】:

          【解决方案5】:
          class Solution:
             def twoSum(self, nums, target):
             """
             :type nums: List[int]
             :type target: int
             :rtype: List[int]
             """
             n = len(nums)
             for i in range(n):
                for j in  range(i+1, n):
                   if nums[i] + nums[j] == target:
                      return [i,j]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-12-23
            • 1970-01-01
            • 2019-03-30
            • 2014-02-18
            • 2014-02-09
            • 1970-01-01
            • 2022-01-04
            • 2015-05-31
            相关资源
            最近更新 更多