-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchFirstAndLastPosition.java
113 lines (101 loc) · 3.02 KB
/
BinarySearchFirstAndLastPosition.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import com.sun.org.apache.xerces.internal.impl.xs.util.XIntPool;
import java.util.Arrays;
public class BinarySearchFirstAndLastPosition {
/*static int[] res=new int[2];
public static int[] searchRange(int[] nums, int target) {
if(nums.length==0)
return new int[]{-1,-1};
doBinarySearch(nums,0,nums.length-1,target);
return res;
}
private static void doBinarySearch(int[] nums, int low, int high, int target) {
if(low>high || high<low) {
res[0]=-1;
res[1]=-1;
return;
}
int mid=low+(high-low)/2;
if(nums[mid]==target){
int first=mid;
int first_Res=mid;
while (true){
first=searchwithRange(nums,0,first-1,target);
if(first!=-1)
first_Res=first;
else
break;
}
int lastRes=mid;
int last=mid+1;
while (true){
last=searchwithRange(nums,last,nums.length,target);
if(last!=-1 && lastRes!=last)
lastRes=last;
else
break;
}
res[0]=first_Res;
res[1]=lastRes;
return;
}
if(nums[mid]<target)
doBinarySearch(nums,mid+1,high,target);
else
doBinarySearch(nums,low,mid-1,target);
}
private static int searchwithRange(int[] nums,int low,int high,int key){
if(low>high || high<low)
return -1;
int mid=low+(high-low)/2;
if(nums[mid]==key)
return mid;
if(nums[mid]<key)
return searchwithRange(nums,mid+1,high,key);
else
return searchwithRange(nums,low,mid-1,key);
}*/
public static void main(String[] args) {
/* *//* int[] nums={5,7,7,8,8,10};
int target=6;*//*
*//*int[] nums={1};
int target=1;*//*
*//*
int target=2;
int[] nums={2,2};*//*
int[] nums={1};
int target=1;
int[] res = searchRange(nums, target);
System.out.println(Arrays.toString(res));
*/
/* int[] nums={5,7,7,8,8,10};
int target=8;*/
int[] nums={1};
int target=1;
int[] res = searchRange(nums, target);
System.out.println(Arrays.toString(res));
}
public static int[] searchRange(int[] nums, int target) {
int first=first_pos(nums,target);
int last=first_pos(nums,target+1)-1;
if (first<=last)
return new int[]{first,last};
else
return new int[]{-1,-1};
}
private static int first_pos(int[] nums,int key){
int low=0;
int high=nums.length-1;
int first_pos=nums.length;
while (low<=high){
int mid=low+(high-low)/2;
if(nums[mid]>=key){
first_pos=mid;
high=mid-1;
}
else {
low=mid+1;
}
}
return first_pos;
}
}