-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34.cpp
More file actions
37 lines (28 loc) · 691 Bytes
/
Copy path34.cpp
File metadata and controls
37 lines (28 loc) · 691 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
30
31
32
33
34
35
36
37
#include <vector>
using namespace std;
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
int n = nums.size();
int start = BS(nums, target, n);
if (start == n || nums[start] != target) {
return {-1, -1};
}
int end = BS(nums, target + 1, n) - 1;
return {start,end};
}
private:
int BS(vector<int>& nums, int target, int n) {
int l = 0;
int r = n;
while (l < r) {
int m = l + (r - l)/2;
if (nums[m] >= target) {
r = m;
} else {
l = m + 1;
}
}
return l;
}
};