就是二分法的使用,只是旋转数组多了一个判断中间mid的值是否大于最左边的值,然后判断从哪里是旋转的。然后再移动left或者right
class Solution:
def search(self , nums , target ):
left = 0
right = len(nums)-1
while left<=right:
mid = (left+right)//2
if nums[mid] == target:
return mid
elif nums[mid] >= nums[left]:
if(nums[mid] > target):
right = mid - 1
else:
left = mid + 1
else:
if(target > nums[mid] and target <= nums[right]):
left = mid + 1
else:
right = mid - 1
return -1