讨论/《二分查找》 - 搜索旋转排序数组/
《二分查找》 - 搜索旋转排序数组
class Solution:
    def search(self, nums: List[int], target: int) -> int:
        if not nums:
            return -1
        
        lo, hi = 0, len(nums)

        while lo < hi:
            mid = lo + (hi - lo) // 2
            # mid, pivot, target
            if nums[mid] >= nums[0] > target:
                lo = mid + 1
            elif nums[mid] < nums[0] <= target:
                hi = mid
            elif nums[mid] > target:
                hi = mid
            elif nums[mid] < target:
                lo = mid + 1
            else:
                return mid
        return -1
展开全部 14 讨论