-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0034.cpp
More file actions
28 lines (28 loc) · 1.01 KB
/
Copy path0034.cpp
File metadata and controls
28 lines (28 loc) · 1.01 KB
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
class Solution {
int binarySearch(vector<int> const &nums, int target, int start, int end) {
if (start > end) return -1;
int center = (start + end) / 2;
if (target > nums[center])
return binarySearch(nums, target, center + 1, end);
if (target < nums[center])
return binarySearch(nums, target, start, center - 1);
return center;
}
public:
vector<int> searchRange(vector<int> &nums, int target) {
int foundIndex = binarySearch(nums, target, 0, nums.size() - 1);
if (foundIndex == -1)
return {-1, -1};
int first = foundIndex, upperBound;
while (first != -1) {
upperBound = first - 1;
first = binarySearch(nums, target, 0, upperBound);
}
int last = foundIndex, lowerBound;
while (last != -1) {
lowerBound = last + 1;
last = binarySearch(nums, target, lowerBound, nums.size() - 1);
}
return {upperBound + 1, lowerBound - 1};
}
};