/***
* 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;
}
}
相关文章
- 04-03p23 二分搜索 (leetcode 704)
- 04-03704. 二分查找
- 04-03leetcode 704.二分查找
- 04-03LeetCode:704.二分查找
- 04-03704. Binary Search
- 04-03704 二分查找
- 04-03二分查找及其变种
- 04-03问题 C: 字符串的查找删除(字符串好题)
- 04-03python进阶练习之——查找字符串❤️
- 04-03C. Magic Ship cf 二分