# 在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums
,和一个目标值 target
。找出给定目标值在数组中的开始位置和结束位置。
如果数组中不存在目标值 target
,返回 [-1, -1]
。
进阶:
- 你可以设计并实现时间复杂度为
O(log n)
的算法解决此问题吗?
示例 1:
输入:nums = [5,7,7,8,8,10], target = 8
输出:[3,4]
示例 2:
输入:nums = [5,7,7,8,8,10], target = 6
输出:[-1,-1]
示例 3:
输入:nums = [], target = 0
输出:[-1,-1]
提示:
0 <= nums.length <= 105
-109 <= nums[i] <= 109
nums
是一个非递减数组
-109 <= target <= 109
以下错误的选项是?
## aop
### before
```cpp
#include
using namespace std;
```
### after
```cpp
int main()
{
Solution sol;
vector res;
vector nums{5, 7, 7, 8, 8, 10};
int target = 8;
res = sol.searchRange(nums, target);
for (auto i : res)
cout << i << " ";
return 0;
}
```
## 答案
```cpp
class Solution
{
public:
int left(vector &nums, int target)
{
int left = 0;
int right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
{
if (mid == 0 || nums[mid - 1] < target)
{
return mid;
}
right = mid + 1;
}
else if (nums[mid] > target)
{
right = mid + 1;
}
else
{
left = mid - 1;
}
}
return -1;
}
int right(vector &nums, int target)
{
int left = 0;
int right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
{
if (mid == nums.size() - 1 || nums[mid + 1] > target)
{
return mid;
}
left = mid - 1;
}
else if (nums[mid] > target)
{
right = mid + 1;
}
else
{
left = mid - 1;
}
}
return -1;
}
vector searchRange(vector &nums, int target)
{
vector result;
result.push_back(left(nums, target));
result.push_back(right(nums, target));
return result;
}
};
```
## 选项
### A
```cpp
class Solution
{
public:
vector searchRange(vector &nums, int target)
{
vector res(2, -1);
int i = 0, j = nums.size();
int mid = (i + j) / 2;
int p = -1;
while (i < j)
{
if (nums[mid] == target)
{
p = mid;
break;
}
if (nums[mid] > target)
{
if (j == mid)
break;
j = mid;
mid = (i + j) / 2;
}
else
{
if (i == mid)
break;
i = mid;
mid = (i + j) / 2;
}
}
if (p == -1)
{
return res;
}
else
{
int a = p, b = p;
while (a > 0 && nums[a - 1] == target)
a--;
while (b < nums.size() - 1 && nums[b + 1] == target)
b++;
vector h;
h.push_back(a);
h.push_back(b);
return h;
}
}
};
```
### B
```cpp
class Solution
{
public:
vector searchRange(vector &nums, int target)
{
vector res;
res.push_back(binary_search_begin(nums, target));
res.push_back(binary_search_end(nums, target));
return res;
}
private:
int binary_search_begin(vector nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target > nums[mid])
{
lo = mid;
}
else
{
hi = mid;
}
}
if (hi == nums.size() || nums[hi] != target)
{
return -1;
}
else
{
return hi;
}
}
int binary_search_end(vector nums, int target)
{
int lo = -1;
int hi = nums.size();
while (lo + 1 < hi)
{
int mid = lo + (hi - lo) / 2;
if (target < nums[mid])
{
hi = mid;
}
else
{
lo = mid;
}
}
if (lo == -1 || nums[lo] != target)
{
return -1;
}
else
{
return lo;
}
}
};
```
### C
```cpp
class Solution
{
public:
vector searchRange(vector &nums, int target)
{
int start = -1, end = -1;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] == target)
{
if (start == -1)
start = i;
end = i;
}
}
vector ans;
ans.push_back(start);
ans.push_back(end);
return ans;
};
};
```