-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search for a Range.java
51 lines (42 loc) · 1.22 KB
/
Search for a Range.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
/*
左边一个bound, 右边一个bound,通过两次binary search来找到index
*/
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] res = {-1, -1};
if(nums == null || nums.length == 0) return res;
int start = 0;
int end = nums.length - 1;
//left bound是用start来记录的
while(start < end){
int mid = start + (end - start) / 2;
if(nums[mid] >= target){
end = mid;
}else{
start = mid + 1;
}
}
if(nums[start] == target){
res[0] = start;
}else{
return res;
}
// right bound 用end来记录
start = 0;
end = nums.length - 1;
while(start < end){
int mid = start + (end -start + 1) / 2;
if(nums[mid] <= target){
start = mid;
}else{
end = mid-1;
}
}
if(nums[end] == target){
res[1] = end;
}
return res;
}
}
// delete the numbers
// keep the former elemnts and move the afterwards elment by (index2 - index2) steps