-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
46 lines (46 loc) · 1.03 KB
/
test.cpp
File metadata and controls
46 lines (46 loc) · 1.03 KB
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
# include<vector>
using namespace std;
class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int left = 0,right = n-1;
int mid;
while(left <= right){
mid = left/2 + right/2;
if(nums[mid] > nums[0])
left = mid;
else if(nums[mid] < nums[0])
right = mid;
else
break;
}
if(target < nums[0]){
left = mid;
right = n -1;
}
else{
right = mid;
left = 0;
}
int result = -1;
while(left<=right){
mid = right/2 + left /2;
if(target > nums[mid])
left = mid;
else if(target < nums[mid])
right = mid;
else{
result = mid;
break;
}
}
return result;
}
};
int main(){
Solution s;
vector <int> num = {4,5,6,7,0,1,2};
s.search(num,0);
return 0;
}