二.二分查找(排序旋转数组)

搜索旋转排序数组

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        if not nums:
            return -1
        l, r = 0, len(nums) - 1
        while l <= r:
            mid = (l + r) // 2
            if nums[mid] == target:
                return mid
            # 左半部分是有序
            if nums[0] <= nums[mid]:
                # target落在左半部分有序区域内
                if nums[0] <= target < nums[mid]:
                    r = mid - 1
                else:
                    l = mid + 1
            else: # 右半部分是有序
                # target落在右半部分有序区域内
                if nums[mid] < target <= nums[len(nums) - 1]:
                    l = mid + 1
                else:
                    r = mid - 1
        return -1

上一篇:使用docker报错 :Error response from daemon: Get https://index.docker.io/v1/search?q=java&n=25: dial tcp:


下一篇:Kubernetes(K8s)基本概念:HPA(Pod横向自动扩容)、StatefulSet