-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchRange.java
More file actions
executable file
·64 lines (58 loc) · 1.75 KB
/
searchRange.java
File metadata and controls
executable file
·64 lines (58 loc) · 1.75 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
58
59
60
61
62
63
64
package array;
/**
* 给定一个已经升序排序的整形数组,找出给定目标值的开始位置和结束位置。
你的算法时间复杂度必须是 O(log n) 级别。
如果在数组中找不到目标,返回 [-1, -1]。
例如:
给出 [5, 7, 7, 8, 8, 10] 和目标值 8,
返回 [3, 4]。
* Created by Administrator on 2018/3/30 0030.
*/
public class searchRange {
public int[] searchRange(int[] nums, int target) {
int[] result=new int[2];
int size=nums.length;
int low=0;int height=size-1;
int middle=(height-low)/2;
int resultLow=-1;
int resultHigh=-1;
boolean flag=false;
while(low<=height){
if(nums[middle]==target) {
flag=true;
break;
}
else if(nums[middle]>target){
height=middle-1;
}
else low=middle+1;
middle=(height+low)/2;
}
if(flag==true){
resultLow=middle;
resultHigh=middle;
int i=middle;
int j=middle;
while (i>=1&&nums[--i]==nums[middle]){
resultLow=i;
}
while (j<size-1&&nums[++j]==nums[middle]){
resultHigh=j;
}
result[0]=resultLow;
result[1]=resultHigh;
return result;
}
else {
result[0]=-1;
result[1]=-1;
return result;
}
}
public static void main(String[] args) {
int []a={2};
searchRange s=new searchRange();
int[] result=s.searchRange(a,2);
System.out.println(result[0]+""+result[1]);
}
}