First Bad Version - Javascript
来源:互联网 发布:pp助手苹果mac 编辑:程序博客网 时间:2024/06/08 08:07
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n
versions [1, 2, ..., n]
and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version)
which will return whether version
is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Tag
Binary Search
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Divide & Conquer
/** * Definition for isBadVersion() * * @param {integer} version number * @return {boolean} whether the version is bad * isBadVersion = function(version) { * ... * }; *//** * @param {function} isBadVersion() * @return {function} */var solution = function (isBadVersion) { /** * @param {integer} n Total versions * @return {integer} The first bad version */ return function (n) { if (n === 1) { return isBadVersion(1) ? 1 : 0; } return solutionHelper(isBadVersion, 1, n); };};function solutionHelper(isBadVersion, start, end) { var mid = Math.ceil((start + end) / 2); if (isBadVersion(mid) && !isBadVersion(mid - 1)) { return mid; } else if (isBadVersion(mid)) { return solutionHelper(isBadVersion, start, mid - 1); } else { return solutionHelper(isBadVersion, mid + 1, end); }}
- First Bad Version - Javascript
- [刷题]First Bad Version
- first bad version
- First Bad Version
- [leetcode]First Bad Version
- First Bad Version
- LeetCode First Bad Version
- [leetcode] First Bad Version
- 278 First Bad Version
- First Bad Version
- [Leetcode]First Bad Version
- Leetcode: First Bad Version
- leetcode First Bad Version
- LeetCode || First Bad Version
- [leetcode]First Bad Version
- Leetcode: First Bad Version
- First Bad Version
- #leetcode#First Bad Version
- cocos2d-x 3.x学习之ShowAction
- Struts2工作原理
- HashMap摘抄
- Android中利用Fragment同时支持屏幕和手机
- Java四种线程池的使用
- First Bad Version - Javascript
- MySQL命令大全
- Codeforces Round #357 (Div. 2) A. A Good Contest
- 页面跳转传参后台乱码的问题
- NEUOJ 711 (字典树)
- 2016年06月14日学习日记c语言结构体
- 画空心圆
- 支持向量机SVM原理篇
- 根据当前日期获取本周日期