-
Notifications
You must be signed in to change notification settings - Fork 8
/
solution.cpp
31 lines (29 loc) · 882 Bytes
/
solution.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
/**
* 88 / 88 test cases passed.
* Runtime: 8 ms
* Memory Usage: 13.3 MB
*/
class Solution {
public:
int lower_bound(const 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) r = mid - 1;
else l = mid + 1;
}
return l < nums.size() && nums[l] == target ? l : -1;
}
int upper_bound(const 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 r >= 0 && nums[r] == target ? r : -1;
}
vector<int> searchRange(vector<int>& nums, int target) {
return {lower_bound(nums, target), upper_bound(nums, target)};
}
};