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).
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
给定一个被旋转过的数组,从中找出指定的元素。对于排好序的数组查找,正常的第一想法便是二分法。本题设置了一个障碍是数组被旋转过,最初的想法是找到这个旋转的分界线,其符合这样的条件;
1、在其左边的都比它小
2、在其右边的也比它小
换句话说,其为数组的最大值。得到该最大值的位置后,我们便可对两部分数组分别进行二分法查找。这种解法是比较直观的一种解法,复杂度是O(N),此种解法我们暂且不表。下面我们来介绍一种O(logN)的算法:
该方法同样是用二分法,对于每次中间元素,我们比较其和最右侧元素的值:
1、若小于最右侧元素,说明中间元素在数组分界线的右侧。若中间元素值小于目标值且目标值小于最右侧元素值,说明目标值在中间位置和最右侧元素中间,否则在中间元素的左侧;
2、若大于最右侧元素,说明中间元素在数组分界线的左侧。若最左侧元素小于目标值且目标值小于中间元素值,说明目标值在中间位置和最左侧元素中间,否则在中间元素右侧。
由此,我们可以得到以下代码:
class Solution {
public:
int search(vector<int>& nums, int target) {
if(0 == nums.size())
return -1;
int left = 0, right = nums.size() - 1;
while(left <= right)
{
int mid = (left + right) / 2;
if(nums[mid] == target)
return mid;
if(nums[mid] < nums[right]) //mid is in the right part
{
if(nums[mid] < target && target <= nums[right])
left = mid + 1;
else
right = mid - 1;
}
else
{
if(nums[left] <= target && target < nums[mid])
right = mid - 1;
else
left = mid + 1;
}
}
return -1;
}
};