Search in rotated Sorted Array
Difficulty: Hard
Suppose a sorted array is rotated on 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 is given a target value to search. If found in the array is return its index, otherwise return-1.
Assume no duplicate exists in the array.
(The next topic will study the existence of duplicate cases search in rotated Sorted Array II)
Problem Solving Ideas:
For array search problems, there are approximately three kinds of time complexity O (n), O (lg N), O (1) o (n), O (lg\ N), O (1)
Where linear complexity O (n) o (n), that is, sequential search, while the constant time O (1) O (1) is generally achieved by keyword index or hash table, and logarithmic complexity O (lg N) O (lg\ N), often by binary lookup, binary search tree type implementation
For the condition in the subject is the deformation of an ordered array, so you can think of "quick sort Selection" – Two-point lookup
the difficulty is to find the dividing element and the recursive condition and its boundary between the left and right sides of the dividing element .
For dividing elements, it is easy to think of directly taking intermediate M = (l+r)/2; array bounds [L, R]
Observation of instances: the ability to find all possible cases must be enhanced
0 1 2 4 5 6 7
4 5 6 7 0 1 2
5 1 3
3 1
Nums[m] > Nums[l]: (L, m-1) single Increment
Nums[m] <= nums[l]: (m+1, R) monocytogenes
Note:
-When the array takes the boundary [L, R], M takes to the center of the right (as in 2 elements, M = 1), so at this time, should compare nums[m], num[l] br>-When the array boundary [L, R] is taken, M is centered on the left (as in 2 elements, M = 0), so at this time, it should be compared to nums[m], num[r]; array bounds are [L,r] –o (lg n) O (lg\ N)
Class Solution {public
:
//nums array boundary is [l,r]
int searchr (vector<int>& nums,int L, int r, int target) {
if (R <= L)
return-1;
int m = (l+r)/2;
if (nums[m] = = target)
return m;
if (Nums[l] < nums[m]) {
if (target >= nums[l] && target < nums[m])
return SEARCHR (Nums, L, M, t arget);
else
return SEARCHR (Nums, m+1, R, Target),
} else {
if (Target > nums[m] && target <= nums[r-1 ])
return SEARCHR (Nums, m+1, R, Target);
else
return SEARCHR (Nums, L, M, target);
}
}
int search (vector<int>& nums, int target) {
return searchr (nums, 0, Nums.size (), target);
}
};
array bounds [L, r]– Note the bounds of the incoming array
Class Solution {public
:
//nums array boundary is [l,r]
int searchr (vector<int>& nums,int L, int r, int target) {
if (R < L)
return-1;
int m = (l+r)/2;
if (nums[m] = = target)
return m;
if (Nums[r] > Nums[m]) {
if (Target > nums[m] && target <= nums[r])
return SEARCHR (Nums, m+1, R, Target);
else
return SEARCHR (Nums, L, M-1, Target),
} else {
if (target >= nums[l] && target < nums[m])
return SEARCHR (Nums, L, m-1, target);
else
return SEARCHR (Nums, m+1, R, Target);
}
}
int search (vector<int>& nums, int target) {
return searchr (nums, 0, Nums.size ()-1, target);
}
};
Sequential search o (n) o (n) – not recommended
Class Solution {public
:
int Search (vector<int>& nums, int target) {
int i = 0;
for (; i < nums.size (); i++) {
if (nums[i] = = target) break
;
if (nums.size () = = i)
return-1;
else
return i;
}
;
(The next topic will study the existence of duplicate cases search in rotated Sorted Array II)