当前位置:网站首页>二分查找-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
};
};
首先还是设置起始值和终点值,开始进行遍历循环,求出中间值,根据中间值的结果去缩小起始和终止值得范围。直到缩小至起始终止相等,这个值就是错误源。值得注意的是在遍历是的判断不能是<=,如果起始值和终止值相等还进入循环,会陷入死循环。
边栏推荐
- y=1/100*100+1/200*200+1/300*300+.....+ 1/m*m
- Teach you to learn dapr - 6 Publish subscription
- [buuctf.reverse] 126-130
- Which low code platform is more friendly to Xiaobai? Here comes the professional evaluation!
- 在国金证券开户怎么样?保障安全吗?
- 防火 疏散 自救…这场安全生产暨消防培训干货满满!
- LeetCode——226. Flip binary tree (BFS)
- SIGIR 2022 | University of Hong Kong and others proposed the application of hypergraph comparative learning in Recommendation System
- mysql Add column 失败 因为之前有数据,不是默认null 不行
- Leetcode 1169. Query invalid transactions (if the amount of data is small, this problem still needs to be solved by violent enumeration)
猜你喜欢

Inspirational. In one year, from Xiaobai to entering the core Department of Alibaba, his counter attack

关于FlowUs这一款国民好笔记

Web3 decentralized storage ecological landscape

Teach you to learn dapr - 4 Service invocation

Redis' 43 serial cannons, try how many you can carry

Byte interview: two array interview questions, please accept

Army chat -- registration of Registration Center

Notes on flowus

VSCode使用 - Remote-SSH 配置说明

What is the difference between digital collections and NFT
随机推荐
MySQL index
Secrets of gear contract
The function keeps the value of variable H to two decimal places and rounds the third digit
【代码随想录-动态规划】T583、两个字符串的删除操作
Redis' 43 serial cannons, try how many you can carry
Leetcode daily [2022 - 02 - 16]
用redis做用户访问数据统计HyperLogLog及Bitmap高级数据类型
What does the equals method compare? Who told you
一起备战蓝桥杯与CCF-CSP之大模拟炉石传说
[C language] static modifies local variables
COMP5216 Mobile Computing Assignment 1 - Extending ToDoList app
The difference between round and truncate in SQL (round or truncate)
Cache breakdown! Don't even know how to write code???
Fire evacuation and self rescue... This safety production and fire training is full!
MySql 导出数据库中的全部表索引
Convert the decimal positive integer m into the number in the forward K (2 < =k < =9) system and output it in bits
She said she was tired! So tired! I want to change my career
Alibaba's "high concurrency" tutorial "basic + actual combat + source code + interview + Architecture" is a god class
[buuctf.reverse] 126-130
并发之Synchronized说明