为什么我的函数没有在这个class中定义?

Why is my function not defined in this class?

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

的 leetcode 问题中实现它

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity.*

Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2

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

NameError: global name 'binary_search' is not defined output = binary_search(nums, target, start, middle, end)

我对 classes 不太熟悉,所以可能是因为我没有做我应该添加 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)

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

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)

您必须使用 self.binary_search(nums, target, start, middle, end) 表示您的功能属于您的 Solution class。