458.Last position of target
1.Description:
2.Code
public int lastPosition(int[] A, int target) {
if(A==null || A.length==0){
return -1;
}
int start=0,end=A.length-1;
while(start+1<end){
int mid=start+(end-start)/2;
if(A[mid]==target){
start=mid;
}
else if(A[mid]>target){
end=mid;
}
else{
start=mid;
}
}
if(A[end]==target){
return end;
}
if(A[start]==target){
return start;
}
return -1;
}Last updated