【问题标题】:Why is my function not defined in this class?为什么我的函数没有在这个类中定义?
【发布时间】:2021-08-23 20:21:38
【问题描述】:

我对创建算法并实际使用它们来解决问题还很陌生,所以我花了几天时间学习什么是二进制搜索,并且我从头开始创建了一个应该可以工作的算法。所以现在我尝试在一个名为“Search insert position”的leetcode问题中实现它:

给定一个由不同整数组成的排序数组和一个目标值,如果找到目标,则返回索引。如果不是,则返回按顺序插入的索引。您必须编写一个具有 O(log n) 运行时复杂度的算法。*

示例 1:输入:nums = [1,3,5,6]target = 5 输出:2

我已经在我的 IDE 上测试了我的程序(没有类 Solution(object):),它适用于提供的测试用例,但由于某种原因,leetcode IDE 给了我一个 p>

NameError:未定义全局名称“binary_search” output = binary_search(nums, target, start, middle, end)

我对类不太熟悉,所以可能是因为我没有做我应该做的事情,比如添加 def __init__(self, ...),但我认为这在 leetcode 中应该没关系。

任何建议或帮助将不胜感激!

这是我的代码:

class Solution(object):
    def binary_search(self, nums, target, start, middle, end):
        if nums[middle] == target:
            return middle
        else:
            while end >= start:
                if nums[middle] > target: # target is smaller than middle index
                    end = middle - 1 # - 1 just incase a value is at index 0
                    middle = (start + end) // 2
                    return(binary_search(nums, target, start, middle, end))
                elif nums[middle] < target: # target is bigger than middle index 
                    start = middle + 1 # + 1 just incase a value is at index [-1] (last index)
                    middle = (start + end) // 2
                    return(binary_search(nums, target, start, middle, end))
        return(middle+1)
    
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        start = 0
        end = len(nums)-1
        middle = end // 2 # middle index not actual middle value
        output = binary_search(nums, target, start, middle, end)
        return(output)

【问题讨论】:

  • 您是否阅读过有关如何定义 python 类的文档?例如。 the official tutorial?
  • 你的意思是self.binary_search(...)
  • 一开始就没有充分的理由将其放在课堂上。这是 LeetCode 的某种要求吗?
  • Solution 几乎可以肯定不需要存在。 Python 不像 Java,Java 要求所有代码都封装在一个类中。
  • 是的,解决方案是问题的必要条件。谢谢@Fred Larson,self.binary_search(...) 成功了!

标签: python class undefined binary-search


【解决方案1】:

您需要使用self,因为binary_search 是此上下文中的类实例属性。试试这个:

class Solution(object):
    def binary_search(self, nums, target, start, middle, end):
        if nums[middle] == target:
            return middle
        else:
            while end >= start:
                if nums[middle] > target: # target is smaller than middle index
                    end = middle - 1 # - 1 just incase a value is at index 0
                    middle = (start + end) // 2
                    return(self.binary_search(nums, target, start, middle, end))
                elif nums[middle] < target: # target is bigger than middle index 
                    start = middle + 1 # + 1 just incase a value is at index [-1] (last index)
                    middle = (start + end) // 2
                    return(self.binary_search(nums, target, start, middle, end))
        return(middle+1)
    
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        start = 0
        end = len(nums)-1
        middle = end // 2 # middle index not actual middle value
        output = self.binary_search(nums, target, start, middle, end)
        return(output)

【讨论】:

  • 其实不是实例属性。
【解决方案2】:

您必须使用self.binary_search(nums, target, start, middle, end) 表示您的函数属于您的Solution 类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-11
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    相关资源
    最近更新 更多