-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathproblem2.cpp
More file actions
32 lines (28 loc) · 734 Bytes
/
Copy pathproblem2.cpp
File metadata and controls
32 lines (28 loc) · 734 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
class Solution {
public:
int search(vector<int>& nums, int target) {
int n=nums.size();
int low=0, high=n-1;
while(low<=high)
{
int mid=low+(high-low)/2;
if(target==nums[mid])
return mid;
if(nums[low]<=nums[mid])
{
if(target>=nums[low] and target<=nums[mid])
high=mid-1;
else
low=mid+1;
}
else
{
if(target>=nums[mid] and target<=nums[high])
low=mid+1;
else
high=mid-1;
}
}
return -1;
}
};