-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#30.cc
More file actions
45 lines (43 loc) · 1.15 KB
/
Copy pathLeetCode#30.cc
File metadata and controls
45 lines (43 loc) · 1.15 KB
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
class Solution {
private:
int getLow(int A[], int n, int target){
int low = 0, high = n-1;
while(low<=high){
int mid = (low+high)/2;
if(target<=A[mid]) high = mid-1;
else low = mid+1;
}
return low;
}
int getHigh(int A[], int n, int target){
int low = 0, high = n-1;
while(low<=high){
int mid = (low+high)/2;
if(target >= A[mid]) low = mid+1;
else high = mid-1;
}
return high;
}
public:
vector<int> searchRange(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> ret;
if(n==0){
ret.push_back(-1);
ret.push_back(-1);
return ret;
}
int left = getLow(A,n,target);
int right = getHigh(A,n,target);
if(left!=-1&&A[left]==target&&right!=-1&&A[right]==target){
ret.push_back(left);
ret.push_back(right);
}
else{
ret.push_back(-1);
ret.push_back(-1);
}
return ret;
}
};