力扣 33. 搜索旋转排序树组

二分查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
def search(nums, target):
if not nums: return -1
# 数组是局部有序的
left, right = 0, len(nums) - 1
while left <= right:
mid = left + right >> 1
if nums[mid] == target:
return mid
# [l, mid - 1] 有序
if nums[0] <= nums[mid]:
if nums[left] <= target < nums[mid]:
right = mid - 1
else:
left = mid + 1
# [mid + 1, right] 有序
else:
if nums[mid] < target <= nums[right]:
left = mid + 1
else:
right = mid - 1
return -1
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信