题解 | #在旋转过的有序数组中寻找目标值#
在旋转过的有序数组中寻找目标值
https://www.nowcoder.com/practice/87c0e7abcbda41e7963660fa7d020995
二分法,每次二分查找有两个判断:1.判断target和nums[mid]的大小关系;2.判断mid落在左边还是右边递增区间。
class Solution: def search(self , nums: List[int], target: int) -> int: # write code here left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if(nums[mid] == target): return mid; elif(target < nums[mid]): if(nums[mid] >= nums[left] and nums[left] <= target or nums[mid] < nums[left]): right = mid - 1 else: left = mid + 1 else: if(nums[mid] <= nums[right] and nums[right] >= target or nums[mid] > nums[left]): left = mid + 1 else: right = mid - 1 return -1