题解 | #牛群的位置排序#
牛群的位置排序
https://www.nowcoder.com/practice/87c81fa27b9a45c49ed56650dfa6f51b
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param labels int整型一维数组
* @param target int整型
* @return int整型
*/
public int searchInsert (int[] labels, int target) {
// write code here
int left=0;
int right=labels.length-1;
while(left<=right){
int mid=(left+right)/2;
if(labels[mid]==target) return mid;
else if(labels[mid]>target) right=mid-1;
else left=mid+1;
}
return left;
}
}
简单的二分查找

