class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> res;
if (nums.size() == 0) {
res.push_back(-1);
res.push_back(-1);
return res;
}
int first = getLowerBound(nums, target, 0, nums.size() - 1);
int second = getLowerBound(nums, target + 1, 0, nums.size() - 1) - 1;
if (first < nums.size() && nums[first] == target) {
res.push_back(first);
res.push_back(second);
} else {
res.push_back(-1);
res.push_back(-1);
}
return res;
}
private:
int getLowerBound(vector<int>& nums, int target, int left, int right)
{
if (left > right) {
return left;
}
int mid = (left + right) / 2;
if (nums[mid] < target) {
return getLowerBound(nums, target, mid + 1, right);
} else {
return getLowerBound(nums, target, left, mid - 1);
}
}
};