forked from moranzcw/LeetCode-NOTES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.cpp
48 lines (45 loc) · 964 Bytes
/
solution.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
class Solution
{
public:
vector<int> searchRange(int A[], int n, int target)
{
begin = end = -1;
find(A, 0, n - 1, target);
vector<int> ans;
ans.push_back(begin);
ans.push_back(end);
return ans;
}
private:
int begin;
int end;
void find(int A[], int l, int r, int target)
{
if (l > r)
{
return;
}
int mid = (l + r) >> 1;
if (A[mid] == target)
{
if (mid < begin || begin == -1)
{
begin = mid;
}
if (mid > end)
{
end = mid;
}
find(A, l, mid - 1, target);
find(A, mid + 1, r, target);
}
else if (A[mid] < target)
{
find(A, mid + 1, r, target);
}
else
{
find(A, l, mid - 1, target);
}
}
};