33.搜索旋转排序数组
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。
你可以假设数组中不存在重复的元素。
你的算法时间复杂度必须是 O(log n) 级别。
示例 1: |
---|
输入: nums = [4,5,6,7,0,1,2], target = 0 输出: 4 |
示例 2: |
---|
输入: nums = [4,5,6,7,0,1,2], target = 3 输出: -1 |
将数组一分为二,其中一定有一个是有序的,另一个可能是有序,也能是部分有序。此时有序部分用二分法查找。无序部分再一分为二,其中一个一定有序,另一个可能有序,可能无序。就这样循环.
class Solution {
public:
int search(vector<int>& nums, int target) {
int mid,l=0,h=nums.size()-1;
while(l <= h){
mid = l + (h - l)/2;
if(nums[mid] == target) return mid;
if(nums[l] <= nums[mid]) //左边升序
if(target<nums[mid] && target>=nums[l])
h = mid-1;
else
l = mid+1;
else //右边升序
if(target<=nums[h] && target>nums[mid])
l = mid+1;
else
h = mid-1;
}
return -1;
}
};
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.