当前位置:网站首页>Binary search-2
Binary search-2
2022-06-26 17:55:00 【Dandan's servant】
This question comes from Power button
The title says that when a version is wrong , Later versions have errors , And try to reduce the sending of authentication requests as much as possible , This is obviously a test of dichotomy . Follow 1~100 Guess a number similar .
Ideas :
First set the start and end values 0 and n. I thought of giving priority to the elimination of closeout , Because if the ending is the error source , The number of cycles is the highest , Will traverse to the innermost layer , Conduct whlie loop , Find the intermediate value , Determine whether the intermediate value is wrong , If it's wrong ,( Then judge whether the previous one is wrong , If not , Is the error source , If it is , Set the starting value to the intermediate value ), If the median is correct , Adjust the end value to the intermediate value + 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
}
}
};
};
The code for the official answer seems to be more concise
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
};
};
First, set the start and end values , Start the traversal loop , Find the middle value , Narrow the range of start and end values according to the results of intermediate values . Until it is reduced to the same starting and ending , This value is the error source . It is worth noting that the judgment of traversal yes cannot be <=
, If the start value and the end value are equal, the loop is entered , Will fall into a dead cycle .
边栏推荐
- Redis and database data consistency
- KDD 2022 | how to use comparative learning in cross domain recommendation?
- Live broadcast preview | how can programmers improve R & D efficiency? On the evening of June 21, the video number and station B will broadcast live at the same time. See you or leave!
- #26class中get和set设置
- How pycharm modifies multiline annotation shortcuts
- Comp281 explanation
- ACL 2022 | zero sample multilingual extracted text summarization based on neural label search
- Prometeus 2.34.0 new features
- 二分查找-2
- 你好,现在网上股票开户买股票安全吗?
猜你喜欢
Microservice architecture practice: user login and account switching design, order query design of the mall
How sparksql returns a specific day of the week by date -dayofweek function
pycharm的plt.show()如何保持不关闭
VSCode使用 - Remote-SSH 配置说明
Detailed explanation of dos and attack methods
Plt How to keep show() not closed
牛客网:设计LRU缓存结构 设计LFU缓存结构
Synchronized description of concurrency
Digital signature standard (DSS)
并发之线程安全
随机推荐
Treasure and niche CTA animation material website sharing
牛客网:设计LRU缓存结构 设计LFU缓存结构
KDD 2022 | 如何在跨域推荐中使用对比学习?
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
Use middleware to record slow laravel requests
数据加密标准DES安全性
小程序设置按钮分享功能
你好,现在网上股票开户买股票安全吗?
Problems encountered this week
Concurrent thread safety
I want to know. I am in Zhaoqing. Where can I open an account? Is it safe to open an account online?
The difference between round and truncate in SQL (round or truncate)
Several key points in divorce agreement
【NPOI】C#跨工作薄复制Sheet模板导出Excel
行锁分析和死锁
数据加密标准(DES)概念及工作原理
pycharm的plt.show()如何保持不关闭
LeetCode——226. 翻转二叉树(BFS)
DoS及攻擊方法詳解
Troubleshooting ideas that can solve 80% of faults!