forked from changqing16/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34_c.cpp
More file actions
29 lines (28 loc) · 644 Bytes
/
34_c.cpp
File metadata and controls
29 lines (28 loc) · 644 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
#include <vector>
using namespace std;
class Solution
{
public:
vector<int> searchRange(vector<int> &nums, int target)
{
int n = nums.size();
int l = left(nums, target);
if (l < 0 || l >= n || nums[l] != target)
return {-1, -1};
return {l, left(nums, target + 1) - 1};
}
private:
int left(vector<int> &nums, int target)
{
int n = nums.size(), l = 0, r = n;
while (l < r)
{
int mid = (l + r) / 2;
if (nums[mid] < target)
l = mid + 1;
else
r = mid;
}
return l;
}
};