标签:https ble 方法 个数 int http 利用 二分查找 判断
参考chaibubble的博客:https://blog.csdn.net/chaipp0607/article/details/76977687
排序数组:已经排好序的数组。(一开始不懂是什么意思..)
考察:二分查找
统计一个数字在排序数组中出现的次数。
public class Solution {
public int GetNumberOfK(int [] array , int k) {
//排序数组:排好序的数组
int sum=0;
int l=leftk(array,k,0,array.length-1);
int r=rightk(array,k,0,array.length-1);
if(l>-1&&r>-1){
sum= r-l+1;
}
return sum;
}
public static int leftk(int []arr,int k,int left,int right){
if(left>right){
return -1;
}
int middle=(right+left)/2;
if(arr[middle]==k){
if(middle!=left&&arr[middle-1]!=k||middle==left){
return middle;
}
else right=middle-1;
}
else if(arr[0]<=arr[arr.length-1]){
if(arr[middle]>k){
right=middle-1;
}
else if(arr[middle]<k){
left=middle+1;
}
}
else if(arr[0]>arr[arr.length-1]){//假设当时排序数组是由大到小的,不写也可以的
if(arr[middle]>k){
left=middle-1;
}
else if(arr[middle]<k){
right=middle+1;
}
}
return leftk(arr,k,left,right);
}
public static int rightk(int []arr,int k,int left,int right){
if(left>right){
return -1;
}
int middle=(right+left)/2;
if (arr[middle]==k){
if(middle!=right&&arr[middle+1]!=k||middle==right){
return middle;
}
else{
left=middle+1;
}
}
else if(arr[0]<=arr[arr.length-1]){
if(arr[middle]>k){
right=middle-1;
}
else {
left=middle+1;
}
}
else if(arr[0]>arr[arr.length-1]){//假设当时排序数组是由大到小的,不写也可以的
if(arr[middle]>k){
left=middle+1;
}
else{
right=middle-1;
}
}
return rightk(arr,k,left,right);
}
}
标签:https ble 方法 个数 int http 利用 二分查找 判断
原文地址:https://www.cnblogs.com/dongmm031/p/12222932.html