class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
vector<int> result;
int tmp = 0;
int left = 0, right = nums.size(), mid = 0;
//if (left == right) return nums[0] == target ? vector<int>{0, 0} : vector<int>{ -1, -1 };
while (left<right){
mid = (left + right) / 2;
if (nums[mid] == target){
tmp = mid;
while (mid>0&&nums[mid - 1] == target) mid--;
while (tmp<nums.size()-1&&nums[tmp + 1] == target) tmp++;
return vector<int> {mid, tmp};
}
else if (nums[mid]<target){
left = mid + 1;
}
else{
right = mid;
}
}
return vector<int> {-1, -1};
}
};
Search for a Range
最新推荐文章于 2024-09-03 18:29:17 发布