-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy path034SearchForARange.java
More file actions
57 lines (46 loc) · 1.38 KB
/
034SearchForARange.java
File metadata and controls
57 lines (46 loc) · 1.38 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
// Author: Li Long, [email protected]
// Date: Apr 17, 2014
// Source: http://oj.leetcode.com/problems/search-for-a-range/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/22893675
// 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 SearchForARange {
public int[] searchRange(int[] A, int target) {
int left = 0;
int right = A.length - 1;
int[] result = { -1, -1 };
while (left <= right) {
int mid = (left + right) / 2;
if (A[mid] > target) {
right = mid - 1;
} else if (A[mid] < target) {
left = mid + 1;
} else {
result[0] = mid;
result[1] = mid;
int i = mid - 1;
while (i >= 0 && A[i] == target) {
result[0] = i;
--i;
}
i = mid + 1;
while (i < A.length && A[i] == target) {
result[1] = i;
++i;
}
break;
}
}
return result;
}
public static void main(String[] args) {
int[] A = { 1, 3, 4, 5, 5 };
SearchForARange slt = new SearchForARange();
int[] result = slt.searchRange(A, 5);
System.out.println(result[0] + " " + result[1]);
}
}