Title description
Assume that the array sorted in ascending order is rotated at a point unknown in advance.
(For example, the array [0,1,2,4,5,6,7] may become [4,5,6,7,0,1,2]).
Search for a given target value, if the target value exists in the array, return its index, otherwise return -1.
You can assume that there are no duplicate elements in the array.
The time complexity of your algorithm must be O(log n) level.
Example
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
answer
Idea: First find the best value, divide the rotating array into two ordered arrays, and then find the target value in the ordered array.
1. First find the index of the smallest value in the rotated sorted array, so that the array can be divided into two segments in ascending order.
2. According to the relationship between nums[0] and target, determine whether target is in the left or right segment, and then perform a binary search on the ascending array.
Reference to the Author: sweetiee
link: https: //leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/duo-si-lu-wan-quan-gong-lue-bi-xu-miao -dong-by-swe/
Code
class Solution {
public:
//, int target
int search(vector<int>& nums, int target) {
int result;
int len = nums.size();
if(len==1){
if(nums[0]==target) return 0;
else return -1;
}
int minInd = minIndex(nums);
//cout << minInd << endl;
if (target <= nums[len-1])
result = binSearch(nums, minInd, len-1, target);
else
result = binSearch(nums, 0, minInd-1, target);
return result;
}
int minIndex(vector<int>& nums) {
int len = nums.size();
int lo = 0;
int hi = len - 1;
int mi;
int minInd;
while (lo < hi) {
mi = (lo + hi) >> 1;
//此判断语句为判断出口
//当mi==lo时,表明lo和hi的距离为0或者1
//最小值为nums[lo]或者nums[hi]
if (mi == lo) {
if (nums[lo] < nums[hi]) minInd = lo;
else minInd = hi;
break;
}
//由于数组中不存在重复元素
//此种情况下,最小值存在于(mi,hi]范围内(反正法可以证明)
if (nums[lo] < nums[mi] && nums[mi] > nums[hi]) {
lo = mi + 1;
}
//此种情况下,最小值存在于(lo,mi]范围内(反正法可以证明)
else if (nums[lo] > nums[mi] && nums[mi] < nums[hi]) {
hi = mi;
}
//此种情况下,最小值存在于[lo,mi)范围内
//此时lo,mi,hi为单调递增
else if (nums[lo] < nums[mi] && nums[mi] < nums[hi]) {
hi = mi;
}
}
return minInd;
}
int binSearch(vector<int>& nums, int lo, int hi, int e) {
while (lo < hi) {
int mi = (lo + hi) >> 1;
if (e < nums[mi]) hi = mi;
else if (nums[mi] < e) lo = mi + 1;
else return mi;
}
if(e==nums[lo]) return lo;
else return -1;
}
};
notes
Due to the different versions of leetcode compiler, if the minIndex function is written as follows,
int minIndex(vector<int>& nums) {
int len = nums.size();
int lo = 0;
int hi = len - 1;
int mi;
while (lo < hi) {
mi = (lo + hi) >> 1;
if (mi == lo) {
if (nums[lo] < nums[hi]) return lo;
else return hi;
}
if (nums[lo] < nums[mi] && nums[mi] > nums[hi]) {
lo = mi + 1;
}
else if (nums[lo] > nums[mi] && nums[mi] < nums[hi]) {
hi = mi;
}
else if (nums[lo] < nums[mi] && nums[mi] < nums[hi]) {
hi = mi;
}
}
}
The output will report an error, but no error will be reported in VS2019.