-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
72 lines (70 loc) · 1.79 KB
/
Solution.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
65
66
67
68
69
70
71
72
public class Solution {
public int[] searchRange(int[] nums, int target) {
int begin=0;
int end=nums.length-1;
int a=(begin+end)/2;
int[] answer=new int[2];
while (nums[a]!=target) {
if(begin==end){
answer[0]=-1;
answer[1]=-1;
return answer;
}
if (nums[a]>target) {
if (a==end) {
end--;
}else {
end=a;
}
}else {
if (a==begin) {
begin++;
}else {
begin=a;
}
}
a=(begin+end)/2;
}
int bb=0;
int be=a;
int ba=(bb+be)/2;
while (!(nums[ba]==target&&(ba==0||nums[ba-1]!=target))) {
if (nums[ba]>=target) {
if (ba==be) {
be--;
}else {
be=ba;
}
}else {
if (ba==bb) {
bb++;
}else {
bb=ba;
}
}
ba=(bb+be)/2;
}
int eb=a;
int ee=nums.length-1;
int ea=(eb+ee)/2;
while (!(nums[ea]==target&&(ea==nums.length-1||nums[ea+1]!=target))) {
if (nums[ea]<=target) {
if (ea==eb) {
eb++;
}else {
eb=ea;
}
}else {
if (ea==ee) {
ee--;
}else {
ee=ea;
}
}
ea=(eb+ee)/2;
}
answer[0]=ba;
answer[1]=ea;
return answer;
}
}