File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int > searchRange (int A[], int n, int target) {
4
+ // Start typing your C/C++ solution below
5
+ // DO NOT write int main() function
6
+
7
+ vector<int > range;
8
+ range.push_back (find_lower_bound (A, n, target));
9
+ range.push_back (find_upper_bound (A, n, target));
10
+ return range;
11
+ }
12
+
13
+ int find_lower_bound (int A[], int n, int target) {
14
+ int left = 0 , right = n - 1 ;
15
+ while (left <= right) {
16
+ int mid = left + (right - left) / 2 ;
17
+ if (A[mid] >= target) {
18
+ right = mid - 1 ;
19
+ }
20
+ else {
21
+ left = mid + 1 ;
22
+ }
23
+ }
24
+ if (A[left] == target)
25
+ return left;
26
+ return -1 ;
27
+ }
28
+
29
+ int find_upper_bound (int A[], int n, int target) {
30
+ int left = 0 , right = n - 1 ;
31
+ while (left <= right) {
32
+ int mid = left + (right - left) / 2 ;
33
+ if (A[mid] <= target) {
34
+ left = mid + 1 ;
35
+ }
36
+ else {
37
+ right = mid - 1 ;
38
+ }
39
+ }
40
+ if (A[right] == target)
41
+ return right;
42
+ return -1 ;
43
+ }
44
+ };
You can’t perform that action at this time.
0 commit comments