-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path033.cpp
46 lines (45 loc) · 930 Bytes
/
033.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
class Solution {
public:
int search(vector<int>& nums, int target) {
int l=0, r=nums.size()-1, m;
while(l<=r){
m = (l+r)/2;
if(nums[m]==target) return m;
else if(nums[m]<target){
if(nums[m]<nums[r] && target<=nums[r]) l=m+1;
else r=m-1;
}
else{
if(nums[l]<nums[m] && target>=nums[l]) r=m-1;
else l=m+1;
}
}
return -1;
}
};
/*
- A[l]<target<A[r] ensures the sub is ascending;
- return method is worth learning.
- first ensure the sub is ascending.
- correct, AC
*/
class Solution {
public:
int search(vector<int>& nums, int target) {
int l=0, r=(int)nums.size()-1, m;
if(nums.size()==0) return -1;
while(l<=r){
m = (l+r)/2;
if(nums[m]==target) return m;
else if(nums[m]<nums[r]){//diff
if(nums[m]<target && target<=nums[r]) l=m+1;//diff
else r=m-1;
}
else{
if(target<nums[m] && target>=nums[l]) r=m-1;//diff
else l=m+1;
}
}
return -1;
}
};