-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path34.java
More file actions
28 lines (28 loc) · 700 Bytes
/
34.java
File metadata and controls
28 lines (28 loc) · 700 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
class Solution {
public int[] searchRange(int[] nums, int target) {
int [] ans={-1,-1};
int start=bin(nums,target,true);
int end=bin(nums,target,false);
return new int[] {start,end};
}
int bin(int[] nums, int target,boolean start){
int s=0,e=nums.length-1,ans=-1;
while(s<=e){
int m=(s+e)/2;
if(target>nums[m]){
s=m+1;
}
else if(target<nums[m]){
e=m-1;
}
else{
ans=m;
if(start){
e=m-1;
}
else s=m+1;
}
}
return ans;
}
}