33. Search in Rotated Sorted Array
Problem
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Related Topics:
Array
Binary Search
Analysis
方法一:先找出最小值所在的下标,确定目标在哪一边,然后进行二分查找。
方法二:当 target < nums[mid]
时,存在三种情况。
两则都在分割线(最小值与最大值之间)的左边:为升序,
target
在nums[mid]
左边,所以nums[mid]
右边的元素舍弃,即end = mid - 1
。两则都在分割线的右边:为升序,同上,
end = mid - 1
。由于分割线左边元素大于右边,所以
target
在右边,nums[mid]
在左边:舍弃nums[mid]
左边元素,即start = mid + 1
。
同理,target > nums[mid]
时,也有三种情况,处理方法类似。
Code
找最小值:
所有情况判断:
Last updated