-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch_rotateArr.cpp
More file actions
39 lines (37 loc) · 976 Bytes
/
Copy pathSearch_rotateArr.cpp
File metadata and controls
39 lines (37 loc) · 976 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
31
32
33
34
35
36
37
38
39
/* Leetcode 33 */
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> nums={1,3};
int target = 0;
int left=0, right = nums.size()-1;
if (!right) return nums[left]==target? left: -1;
else{
while(left <= right){
int mid = (left+right)/2;
if(nums[left] <= nums[mid]){
// left array got order
if(nums[left] <= target && target <= nums[mid]){
right = mid -1;
}else{
left = mid + 1;
}
}
else{
// right array got order
if(nums[right] >= target && target >= nums[mid]){
left = mid + 1;
}else{
right = mid -1;
}
}
}
if(target == nums[left]){
cout << left;
}else{
cout << -1;
}
}
return 0;
}