-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy path34-moran991231.java
More file actions
53 lines (49 loc) · 911 Bytes
/
34-moran991231.java
File metadata and controls
53 lines (49 loc) · 911 Bytes
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
class Solution {
public static int[] searchRange(int[] nums, int target) {
int s = 0, e = nums.length - 1, idx;
int[] ret = { -1, -1 };
if (e < 0)
return ret;
if (e == 0) {
ret = (nums[0] == target) ? new int[2] : ret;
return ret;
}
while (s <= e) {
idx = (s + e) / 2;
if (nums[idx] <= target) {
if (s == idx) {
if (nums[idx + 1] == target)
ret[1] = idx + 1;
else if (nums[idx] == target)
ret[1] = idx;
else
return ret;
break;
}
s = idx;
} else {
e = idx - 1;
}
}
if (e < s)
return ret;
s = 0;
e = nums.length - 1;
while (s <= e) {
idx = (s + e+1) / 2;
if (target <= nums[idx]) {
if (e == idx) {
if (nums[idx - 1] == target)
ret[0] = idx - 1;
else if (nums[idx] == target)
ret[0] = idx;
break;
}
e = idx;
} else {
s = idx + 1;
}
}
return ret;
}
}