-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search for a Range.java
47 lines (44 loc) · 1.06 KB
/
Search for a Range.java
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
/*
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[] rst = {-1, -1};
int index = binarySearch(A, 0, A.length-1, target);
if (index == -1) {
return rst;
}
rst = {index, index};
for (int i = index; i >= 0; i--) {
if (A[i] != target) {
break;
}
rst[0] = i;
}
for (int i = index; i < A.length; i++) {
if (A[i] != target) {
break;
}
rst[1] = i;
}
return rst;
}
private int binarySearch(int[] A, int start, int end, int target) {
if (start == end && A[start] != target) {
return -1;
}
int mid = (start + end) / 2;
if (target == A[mid]) {
return mid;
} else if (target > A[mid]) {
return binarySearch(A, mid+1, end, target);
} else {
return binarySearch(A, start, mid, target);
}
}
}