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