-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution34.java
More file actions
35 lines (33 loc) · 796 Bytes
/
Copy pathsolution34.java
File metadata and controls
35 lines (33 loc) · 796 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
public class solution34 {
public int[] searchRange(int[] nums, int target) {
if(nums == null||nums.length== 0)
{
return new int[]{-1,-1};
}
int first = BinarySearch(nums,target);
if(nums[first]!=target)
{
return new int[]{-1,-1};
}
int last = BinarySearch(nums,target+1);
if(nums[last]!=target)
last--;
return new int[]{first,last};
}
private int BinarySearch(int[] nums,int target)
{
int l =0;
int h = nums.length -1;
while(l<h)
{
int m = l + (h-l)/2;
if(nums[m] >= target)
{
h= m ;
}else{
l = m+1;
}
}
return l;
}
}