search_rotated_sorted_array

  • 2022-12-14
  • 浏览 (542)

search_rotated_sorted_array.py 源码

from typing import List


# 搜索旋转排序数组

class Solution:

    def search(self, nums: List[int], target: int) -> int:
        lo, hi = 0, len(nums)-1
        while lo <= hi:
            mid = (lo + hi) // 2
            if nums[mid] == target:
                return mid

            if nums[0] <= nums[mid]:
                if nums[0] <= target < nums[mid]:
                    hi = mid - 1
                else:
                    lo = mid + 1
            else:
                if nums[mid] < target <= nums[-1]:
                    lo = mid + 1
                else:
                    hi = mid - 1
        return -1

你可能感兴趣的文章

find_peak_element

first_last_sort_array

perfect_square

0  赞