forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchForRange.java
64 lines (58 loc) · 1.76 KB
/
searchForRange.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Source : https://oj.leetcode.com/problems/search-for-a-range/
// Inspired by : http://www.jiuzhang.com/solutions/search-for-a-range/
// Author : Lei Cao
// Date : 2015-10-03
/**********************************************************************************
*
* 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].
*
*
**********************************************************************************/
package searchForRange;
public class searchForRange {
public int[] searchRange(int[] nums, int target) {
int[] result = {-1, -1};
if (nums == null) {
return result;
}
int start = 0;
int end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] >= target) {
end = mid;
} else if (nums[mid] < target) {
start = mid;
}
}
if (nums[start] == target) {
result[0] = start;
} else if (nums[end] == target) {
result[0] = end;
}
start = 0;
end = nums.length - 1;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (nums[mid] <= target) {
start = mid;
} else {
end = mid;
}
}
if (nums[end] == target) {
result[1] = end;
} else if (nums[start] == target) {
result[1] = start;
}
return result;
}
}