-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path33.search_in_a_rotated_array.cpp
59 lines (53 loc) · 1.76 KB
/
33.search_in_a_rotated_array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//coding:utf-8
/***********************************************************
Program: Search in a rotated array
Description:
Shanbo Cheng: [email protected]
Date: 2016-07-29 14:13:10
Last modified: 2016-08-19 08:37:54
GCC version: 4.9.3
***********************************************************/
#include <vector>
class Solution {
//firstly, binary search to find the rotation index, then binary search the two parts (could search only one too)
int findMin(vector<int>& nums, int l, int r){
if(nums.size() == 1)
return 0;
if (l < 0 || r < 0 || l >= nums.size() || r >= nums.size())
return -1;
int mid = l + (r - l) >> 1;
if (l <= r){
if (mid == 0)
return nums[mid] > nums[mid + 1] ? mid + 1 : -1;
if (mid == nums.size() - 1)
return nums[mid] < nums[mid - 1] ? mid : -1;
if (nums[mid] <= nums[mid - 1] && nums[mid] <= nums[mid + 1])
return mid;
else
return max(findMin(nums, l, mid - 1), findMin(nums, mid + 1, r));
}
return -1;
}
int binarySearch(vector<int>& nums, int l, int r, int& target){
int m = (l + r)/2;
while(l <= r){
if(nums[m] == target)
return m;
else if(nums[m] > target){
r = m - 1;
m = l + (r - l) >> 1;
}else{
l = m + 1;
m = l + (r - l) >> 1;
}
}
return -1;
}
public:
int search(vector<int>& nums, int target) {
int index = findMin(nums, 0, nums.size() - 1);
if(index == -1)
return binarySearch(nums, 0, nums.size() - 1, target);
return max(binarySearch(nums, 0, index - 1, target), binarySearch(nums, index, nums.size() - 1, target));
}
};