二分查找
程序员文章站
2022-03-15 10:16:58
...
/**
* 存在则返回下标,反之则返回-1
* @param arr
* @param target
* @return
*/
public int binarySearch(int[] arr, int target) {
int low = 0;
int high = arr.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (target == arr[mid]) {
return mid;
} else if (target < arr[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
上一篇: 利用阿里EasyExcel实现excel的生成并下载
下一篇: 百度地图扩展动画maker