文章目录
1、前言
今天是二分查找的最后一更,来做一下LeetCode中的探索的题~
下面一起来看看吧!!!
如何识别二分查找?
二分查找是一种在每次比较之后将查找空间一分为二的算法。每次需要查找集合中的索引或元素时,都应该考虑二分查找。如果集合是无序的,我们可以总是在应用二分查找之前先对其进行排序。
二分查找一般由三个主要部分组成:
- 预处理 —— 如果集合未排序,则进行排序。
- 二分查找 —— 使用循环或递归在每次比较后将查找空间划分为两半。
- 后处理 —— 在剩余空间中确定可行的候选者。
2、二分查找(LeetCode 704)
再也不怕女朋友问我二分查找了!!!【手绘漫画】面试必考之二分查找中回(修订版),(LeetCode 704题)
3、x 的平方根(LeetCode 69)
【手绘漫画】图解LeetCode之x 的平方根(LeetCode 69题)
4、猜数字大小(LeetCode 374)
【手绘漫画】图解LeetCode之猜数字大小(LeetCode 374题)
5、第一个错误的版本(LeetCode 278)
【手绘漫画】图解LeetCode之第一个错误的版本(LeetCode 278题)
6、寻找峰值(LeetCode 162)
【手绘漫画】图解LeetCode之寻找峰值(LeetCode 162题)
7、寻找旋转排序数组中的最小值(LeetCode153)
这个之前讲过!
【手绘漫画】图解LeetCode之寻找旋转排序数组中的最小值(LeetCode153题)
修正版代码:
int findMin(int* nums, int numsSize){
int left=0;
int right=numsSize-1;
while(left<right){
int mid=left+right >> 1;
if(nums[mid]<nums[numsSize-1]){
right=mid;
}
else{
left=mid+1;
}
}
return nums[left];
}
按照模板一,规规矩矩的,没毛病。
8、总结
其实整体看一下,两个模板基本没啥大区别,,,主要还是熟练问题。
目前存疑的问题还有 left
和 right
的设置问题,感觉根据不同的题设计起来比较随意。
再就是注意返回值,是 -1,还是某个下标,再或者数组的元素。
如果有幸帮到你,请帮我点个【赞】,给个【关注】!如果能顺带【评论】给个鼓励,我将不胜感激。
如果想要更多的资源,欢迎关注 @我是管小亮,文字强迫症MAX~