704. 二分查找

/***
* 704. 二分查找
* https://leetcode-cn.com/problems/binary-search/
* date:2021/08/06
* 笔记:二分查找的模板题
*/
public class _704_二分查找 {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length-1;
int mid = (left+right)/2;
while(nums[mid]!=target&&left<right){
if(nums[mid]<target){
left = mid+1;
}else if(nums[mid]>target){
right = mid-1;
}
mid = (left+right)/2;
}
if(nums[mid]==target) return mid;
return -1;
}
}
上一篇:704 二分查找


下一篇:704. Binary Search