-
Notifications
You must be signed in to change notification settings - Fork 3
/
34.cpp
29 lines (29 loc) · 930 Bytes
/
34.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
class Solution {
public:
int binaryStartSearch(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] >= target) right = mid;
else left = mid + 1;
}
return left;
}
int binaryEndSearch(vector<int>& nums, int target) {
int left = 0;
int right = nums.size();
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > target) right = mid;
else left = mid + 1;
}
return left;
}
vector<int> searchRange(vector<int>& nums, int target) {
int left = binaryStartSearch(nums, target);
if (left == nums.size() || nums[left] != target) return {-1, -1};
int right = binaryEndSearch(nums, target) - 1;
return {left, right};
}
};