-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc_34.java
More file actions
43 lines (36 loc) · 1.16 KB
/
lc_34.java
File metadata and controls
43 lines (36 loc) · 1.16 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
public class lc_34 {
public int[] searchRange(int[] nums, int target) {
int[] ans={-1,-1};
int start=search(nums,target,true);
int end=search(nums,target,false);
ans[0]=start;
ans[1]=end;
return ans;
}
int search(int[] nums,int target,boolean first){
int ans=-1;
int start=0;
int n=nums.length;
int end=n-1;
while(start<=end){
int mid=start+(end-start)/2;
if(nums[mid]<target){
start=mid+1;
}
else if(nums[mid]>target){
end=mid-1;
}
else{
ans=mid;
System.out.println(ans);
if(first){
end=mid-1;
}
else{
start=mid+1;
}
}
}
return ans;
}
}