forked from stattrak-dragonlore/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search_for_a_Range.cpp
105 lines (92 loc) · 2.19 KB
/
Search_for_a_Range.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Search for a Range
Given a sorted array of integers, find the
starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
*/
class Solution {
public:
//false, false, false, true, true,
//find the first e, such that a[e] >= target
int lowerbound(int a[], int n, int target)
{
int lo = 0, hi = n - 1;
int mid;
while (lo < hi) {
mid = (lo + hi) / 2;
if (a[mid] >= target) {
hi = mid;
} else {
lo = mid + 1;
}
}
if (a[lo] < target)
return -1;
return lo;
}
//find the first e, such that a[e] > target
int upperbound(int a[], int n, int target)
{
int lo = 0, hi = n - 1;
int mid;
while (lo < hi) {
mid = (lo + hi) / 2;
if (a[mid] > target) {
hi = mid;
} else {
lo = mid + 1;
}
}
if (a[lo] <= target) {
return -1;
}
return lo;
}
class Solution {
public:
vector<int> searchRange(int A[], int n, int target) {
int start = lowerbound(A, n, target);
int end = upperbound(A, n, target);
if (start != -1) {
if (A[start] != target)
start = -1;
}
if (end == -1) {
if (A[n-1] == target)
end = n-1;
} else {
if (A[end-1] == target)
end--;
else
end = -1;
}
vector<int> result(2);
result[0] = start;
result[1] = end;
return result;
}
};
/* using STL */
vector<int> searchRange(int A[], int n, int target) {
int start = lower_bound(A, A+n, target) - A;
int end = upper_bound(A, A+n, target) - A;
if (start == n) {
start = -1;
} else {
if (A[start] != target)
start = -1;
}
if (A[end-1] != target)
end = -1;
else
end--;
vector<int>result(2);
result[0] = start;
result[1] = end;
return result;
}
};