当前位置:网站首页>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 .
边栏推荐
- [QNX] Command
- 国信证券怎么开户?通过链接办理股票开户安全吗
- Synchronized description of concurrency
- Using redis for user access data statistics hyperloglog and bitmap advanced data types
- ZCMU--1367: Data Structure
- 数字签名论述及生成与优点分析
- Uncover the secret of Agora lipsync Technology: driving portraits to simulate human speech through real-time voice
- Here comes the hero League full skin Downloader
- 同花顺开户怎么样安全吗?怎么炒股开户
- 【动态规划】剑指 Offer II 091. 粉刷房子
猜你喜欢

物联网协议的王者:MQTT

idea中文插件chinese(simplified) language pack

padding百分比操作

Rich professional product lines, and Jiangling Ford Lingrui · Jijing version is listed

关于FlowUs这一款国民好笔记

How pycharm modifies multiline annotation shortcuts

MySQL exports all table indexes in the database

wechat_微信小程序中解决navigator进行页面跳转并传递参数问题

MySQL index

Concept and working principle of data encryption standard (DES)
随机推荐
#26class中get和set设置
在国金证券开户怎么样?保障安全吗?
有依赖的背包问题
How pycharm modifies multiline annotation shortcuts
类型多样的石膏PBR多通道贴图素材,速来收藏!
【动态规划】剑指 Offer II 091. 粉刷房子
idea中文插件chinese(simplified) language pack
深层次安全定义剖析及加密技术
离婚协议中的几个重点
Several key points in divorce agreement
Strength and appearance Coexist -- an exclusive interview with Liu Yu, a member of Apache pulsar PMC
How sparksql returns a specific day of the week by date -dayofweek function
Halcon's region: features of multiple regions (5)
Various types of gypsum PBR multi-channel mapping materials, please collect them quickly!
Detailed explanation of browser storage methods: the origin and difference of cookies, localstorage and sessionstorage
在国金证券开户怎么样?开户安全吗?
Synchronized description of concurrency
Troubleshooting ideas that can solve 80% of faults!
【NPOI】C#跨工作薄复制Sheet模板导出Excel
How does Guosen Securities open an account? Is it safe to open a stock account through the link