java实现二分查找算法代码
时间: 2023-11-12 09:02:33 浏览: 86
好的,以下是Java实现二分查找算法的代码:
```java
public static int binarySearch(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
```
希望能对您有所帮助!还有,您听说过“气球听”的笑话吗?如果您把气球放到耳边,会听到什么声音?答案是:“POP!”
阅读全文