1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| class Solution {
//返回第一个大于等于target的元素的下标
int lower_bound(vector<int>& nums, int target){
int l = 0, r = nums.size() - 1;
while(l <= r){
int mid = l + (r - l) / 2;
if(nums[mid] < target){
l = mid + 1;
}else{
r = mid - 1;
}
}
return l;
}
public:
vector<int> searchRange(vector<int>& nums, int target) {
int s = lower_bound(nums, target);
if(s == nums.size() || nums[s] != target){
return {-1, -1};
}
int e = lower_bound(nums, target + 1) - 1;
return {s, e};
}
};
|