-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0033.cpp
More file actions
30 lines (29 loc) · 912 Bytes
/
Copy path0033.cpp
File metadata and controls
30 lines (29 loc) · 912 Bytes
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
class Solution {
int binary_search(const vector<int> &nums, int target, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] > target)
high = mid - 1;
else
low = mid + 1;
}
return -1;
}
public:
int search(vector<int> &nums, int target) {
int pivot = -1;
for (int i = 0; i < nums.size() - 1; i++) {
if (nums[i] > nums[i + 1]) {
pivot = i;
break;
}
}
if (pivot == -1) return binary_search(nums, target, 0, nums.size() - 1);
if (nums[0] <= target)
return binary_search(nums, target, 0, pivot);
else
return binary_search(nums, target, pivot + 1, nums.size() - 1);
}
};