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.
Your algorithm's runtime complexity must be in the order of O(log n).
思路:
需要使用二分法的变体:
这里的数组是首先从小到大排序,然后经过绕着某个轴旋转,比如[0,1,2,4,5,6,7]旋转后得到[4,5,6,7,0,1,2]
那么没法直接使用二分法,我们这里注意到数组之前是从小到大排序的,因为需要输出目标值的索引,那么我们不能去修改那么使用三个flag: left, mid, right
来记录每一次需要搜索的左边界,中间点和右边界:
首先确定边界,不管是递归还是迭代,必须保证left <= right, 在这个前提下,
因为没有重复项,每次搜索前先查看nums[left], nums[mid], nums[right],
若与目标值相等:直接返回
若不相等:
1.比较nums[mid]和nums[right]:
如果nums[mid] < nums[right],说明(mid, right)是有序的:
如果target属于区间(mid, right),就在这个区间接着搜索,重复之前步骤;
如果target不属于区间(mid, right),就在(left, mid)接着搜索,重复之前步骤;
如果nums[mid] > nums[right],
如果nums[mid] < nums[right],说明(left, mid)是有序的:
如果target属于区间(left, mid),就在这个区间接着搜索,重复之前步骤;
如果target不属于区间(left, mid),就在(mid, right)接着搜索,重复之前步骤;
Example 1:
Input: nums = [4,5,6,7,0,1,2]
, target = 0
Output: 4
Example 2:
Input: nums = [4,5,6,7,0,1,2]
, target = 3
Output: -1