-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode-binarysearch-search-for-a-range
128 lines (108 loc) · 3.11 KB
/
lintcode-binarysearch-search-for-a-range
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
https://www.lintcode.com/problem/search-for-a-range/description
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> ans;
int ansl = -1;
for (int l = 0, r = A.size() - 1; l <= r;) {
int mid = l + (r - l) / 2;
if (A[mid] > target) {
r = mid - 1;
}
if (A[mid] < target) {
l = mid + 1;
}
if (A[mid] == target) {
ansl = mid;
r = mid - 1;
}
}
int ansr = -1;
for (int l = 0, r = A.size() - 1; l <= r;) {
int mid = l + (r - l) / 2;
if (A[mid] > target) {
r = mid - 1;
}
if (A[mid] < target) {
l = mid + 1;
}
if (A[mid] == target) {
ansr = mid;
l = mid + 1;
}
}
ans.push_back(ansl);
ans.push_back(ansr);
return ans;
}
};
my turn:【has problem】
class Solution {
public:
/**
* @param A: an integer sorted array
* @param target: an integer to be inserted
* @return: a list of length 2, [index1, index2]
*/
vector<int> searchRange(vector<int> &A, int target) {
// write your code here
//Sol: do the binary search twice
//find the first postion then the last position
int start = 0;
int end = A.size() - 1; //index
vector<int> bound[2];// result; //pre-initialize it with size of 2.
//First, we consider the special case
if(A[start] > target || A[end] < target){
bound[0] = bound[1] = -1;
return bound;
}
//1. search index1 --> first position
while(start + 1 < end){
mid = start + (end - start)*0.5;
if[A[mid] == target]{
end = mid;//左移
}else if(A[mid] < target){
start = mid;
}else{
end = mid
}
}
//check two bound; // for case start + 1 = end;
if(A[start] == target){
bound[0] = start;
}
else if(A[end] == target){
bound[0] == end;
}else{ //start != target && end != target --> we did not find it
bound[0] = bound[1] = -1;
return bound;
}
//2. search index2 --> last position
while(start + 1 < end){
mid = start = (end - start)*0.5;
if(A[mid] == target){
start = mid; //右移
}else if(A[mid] < target){
start = mid;
}else{
end = mid;
}
}
//check the bound // for last position, we check end first
if(A[end] == target){
bound[1] = end;
}else if (A[start] == target){
bound[1] = start;
}else{
bound[0] = bound[1] = -1;
return bound;
}
return bound;
}
};