题目:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., You are given a target value to search. If found in the array return its index, otherwise return 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 = [ Example 2: Input: nums = [ |
假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 你可以假设数组中不存在重复的元素。 你的算法时间复杂度必须是 O(log n) 级别。 示例 1: 输入: nums = [ 示例 2: 输入: nums = [ |
思路:排序数组首先考虑使用二分查找。两种划分,第一种nums[mid]<nums[j],说明后面是有序的,看target是否在这一区间,如果在i=mid+1,不在j=mid-1就在左边找。第二种,nums[mid]>num[j],说明mid前面是有序,看target是否在这一区间,如果在j=mid-1,不在i=mid+1
class Solution {
public:
int search(vector<int>& nums, int target) {
int i=0,j=nums.size()-1,mid;
while(i<=j)
{
mid = (i+j)/2;
if(nums[mid]==target) return mid;
else if(nums[mid]<nums[j])
{
if(nums[mid]<target && target<=nums[j]) i=mid+1;
else j=mid-1;
}else
{
if(nums[i]<=target && target<nums[mid]) j=mid-1;
else i=mid+1;
}
}return -1;
}
};