lc704.二分查找

【递归】

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        if len(nums) == 0:
            return -1
        mid = int(len(nums)-1 / 2) 
        result = 0
        if nums[mid] > target:
            result = self.search(nums[:mid], target)
        elif nums[mid] < target:
            result = self.search(nums[mid+1:], target)
        elif nums[mid] == target:
            return mid
        return result

 

posted @ 2022-04-27 18:20  stronger_el  阅读(17)  评论(0)    收藏  举报