-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSearch_for_a_Range.cpp
75 lines (70 loc) · 2.03 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
/*
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].
Example
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
*/
#include <vector>
using namespace std;
class Solution {
/**
*@param A : an integer sorted array
*@param target : an integer to be inserted
*return : a list of length 2, [index1, index2]
*/
public:
vector<int> searchRange(vector<int>& A, int target) {
// write your code here
vector<int> result(2, -1);
if (A.empty()) {
return result;
}
if (A.front() == target && A.back() == target) {
result[0] = 0;
result[1] = A.size() - 1;
return result;
}
int begin = 0, end = A.size() - 1;
while (begin + 1 < end) {
int mid = begin + (end - begin) / 2;
if (A[mid] == target) {
return searchRange(A, target, mid);
} else if (A[mid] > target) {
end = mid;
} else {
begin = mid;
}
}
if (A[begin] == target) {
return searchRange(A, target, begin);
}
if (A[end] == target) {
return searchRange(A, target, end);
}
return result;
}
private:
vector<int> searchRange(vector<int>& A, int target, int pivot) {
vector<int> result(2, -1);
int resultBegin = pivot, resultEnd = pivot;
while (resultBegin > 0) {
if (A[resultBegin - 1] == target) {
resultBegin--;
} else {
break;
}
}
while (resultEnd < A.size() - 1) {
if (A[resultEnd + 1] == target) {
resultEnd++;
} else {
break;
}
}
result[0] = resultBegin;
result[1] = resultEnd;
return result;
}
};