-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34.cpp
More file actions
26 lines (26 loc) · 752 Bytes
/
34.cpp
File metadata and controls
26 lines (26 loc) · 752 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
class Solution {
public:
int bsearchindex(vector<int>& nums, int target, bool left, int l ,int h){
while(l<h){
int mid = (l+h)/2;
if(nums[mid]>target || (left && target==nums[mid]))
h = mid;
else
l = mid+1;
}
return l;
}
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res = {-1,-1};
if(nums.empty())
return res;
int n = nums.size();
int idx = bsearchindex(nums,target,true,0,n);
if(idx==n || nums[idx]!=target)
return res;
res[0] = res[1] = idx;
idx = bsearchindex(nums,target,false,res[0],n);
res[1] = idx-1;
return res;
}
};