forked from mengli/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch for a Range.java
More file actions
63 lines (56 loc) · 1.7 KB
/
Search for a Range.java
File metadata and controls
63 lines (56 loc) · 1.7 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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].
public class Solution {
public int[] searchRange(int[] A, int target) {
int low = findLow(A, target, 0, A.length - 1);
int high = findHigh(A, target, 0, A.length - 1);
int[] ret = new int[2];
ret[0] = low;
ret[1] = high;
return ret;
}
private int findLow(int[] A, int target, int l, int r) {
int mid = 0;
int ret = -1;
while (l <= r) {
mid = (l + r) / 2;
if (A[mid] == target) {
ret = mid;
int next = findLow(A, target, l, mid - 1);
if (next != -1) {
ret = next;
}
break;
} else if (A[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return ret;
}
private int findHigh(int[] A, int target, int l, int r) {
int mid = 0;
int ret = -1;
while (l <= r) {
mid = (l + r) / 2;
if (A[mid] == target) {
ret = mid;
int next = findHigh(A, target, mid + 1, r);
if (next != -1) {
ret = next;
}
break;
} else if (A[mid] < target) {
l = mid + 1;
} else {
r = mid - 1;
}
}
return ret;
}
}