当前位置:网站首页>二分查找-2
二分查找-2
2022-06-26 17:23:00 【丹丹的小跟班】
本题来自力扣
题目说当一个版本错误后,后面的版本都出错,并且要尽可能的减少验证请求的发送,这就明显是一个考验二分法的题目。跟1~100猜一个数类似。
思路:
首先设置起始值和终止值0和n。我想到优先将收尾排除,因为如果是收尾为错误源的话,循环的次数是最多的,会遍历至最里面一层,进行whlie循环,求中间值,判断中间值是否是错误的,如果是错误的,(再进行判断前一个是否是错误,如果不是,则为错误源,如果是,则将起始值设置为中间值),如果中间值是正确的,就调整终止值为中间值 + 1。
var solution = function(isBadVersion) {
return function(n) {
let start = 0
let end = n
if(isBadVersion(start)) return start
if(isBadVersion(end) && !isBadVersion(end - 1)) return end
while(start <= end) {
let min = Math.floor((end - start) / 2) + start
if(isBadVersion(min)) {
if(!isBadVersion(min - 1)) {
return min
}
end = min
}else {
start = min + 1
}
}
};
};
官方答案的代码似乎更为简洁
var solution = function(isBadVersion) {
return function(n) {
let start = 0
let end = n
while(start < end) {
let min = Math.floor((end - start) / 2) + start
if(isBadVersion(min)) {
end = min
}else {
start = min + 1
}
}
return start
};
};
首先还是设置起始值和终点值,开始进行遍历循环,求出中间值,根据中间值的结果去缩小起始和终止值得范围。直到缩小至起始终止相等,这个值就是错误源。值得注意的是在遍历是的判断不能是<=
,如果起始值和终止值相等还进入循环,会陷入死循环。
边栏推荐
- 宝藏又小众的CTA动画素材素材网站分享
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- Halcon's region: features of multiple regions (5)
- sql中ROUND和TRUNCATE的区别(四舍五入还是截取小数点后几位)
- SIGIR 2022 | 港大等提出超图对比学习在推荐系统中的应用
- 10 cloud security best practices that enterprises need to know
- The difference between round and truncate in SQL (round or truncate)
- Jouer avec Linux et installer et configurer MySQL facilement
- Secrets of gear contract
- Count the number of each vowel letter in the string
猜你喜欢
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
VSCode使用 - Remote-SSH 配置说明
vue--vuerouter缓存路由组件
Here comes the hero League full skin Downloader
Vue--vuerouter cache routing component
关于FlowUs这一款国民好笔记
【万字总结】以终为始,详细分析高考志愿该怎么填
20: Chapter 3: develop the pass service: 3: get through the redis server in the program; (it only connects with the redis server and does not involve specific business development)
Platform management background and merchant menu resource management: Design of platform management background data service
对NFT市场前景的7个看法
随机推荐
halcon之区域:多种区域(Region)特征(5)
有依赖的背包问题
COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
Teach you to learn dapr - 4 Service invocation
vue--vuerouter缓存路由组件
Discussion: the next generation of stable coins
Discover K8E: minimalist kubernetes distribution
Leetcode - 226. Retourner l'arbre binaire (bfs)
Secrets of gear contract
ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
Deployment and operation of mongodb partitioned cluster
Leetcode topic [array] -283- move zero
Introduction to minimal API
合约量化系统开发方案详细,量化合约系统开发技术说明
Cache breakdown! Don't even know how to write code???
Can Luo Yonghao succeed in entering the AR field this time?
关于FlowUs这一款国民好笔记
Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack
Community ownership of NFT trading market is unstoppable
In those years, interview the abused red and black trees