LeetCode278 find the first bad version
来源:互联网 发布:mac电脑打不开照片权限 编辑:程序博客网 时间:2024/05/21 22:51
Describtion
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
解题思路:
使用binary search的方法进行处理
该题目应该是属于简单题,直接上代码
/** * public class SVNRepo { * public static boolean isBadVersion(int k); * } * you can use SVNRepo.isBadVersion(k) to judge whether * the kth code version is bad or not.*/class Solution { /** * @param n: An integers. * @return: An integer which is the first bad version. */ public int findFirstBadVersion(int n) { // write your code here if (n == 0) { return 0; } int start = 1; int end = n; while (start + 1 < end) { int mid = start + (end - start) / 2; if (SVNRepo.isBadVersion(mid)) { end = mid; } else { start = mid; } } if (SVNRepo.isBadVersion(start)) { return start; } if (SVNRepo.isBadVersion(end)) { return end; } return -1; }}
0 0
- LeetCode278 find the first bad version
- leetcode278:First Bad Version
- leetcode278---First Bad Version
- LeetCode278. First Bad Version
- Leetcode278. First Bad Version
- Leetcode278. First Bad Version
- leetcode278. First Bad Version
- LeetCode278. First Bad Version(二分)
- LeetCode278——First Bad Version(二分查找+一个注意点)
- leetcode278.First Bad Version 关于有符号数加法溢出的错误
- [刷题]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
- c语言计算器
- 微信小程序 出现未找到入口 app.json文件
- 个人网站再次遭受CC攻击总结—wdcp环境下抵御CC攻击的方法
- Ember 翻译——教程十二:部署
- 软件测试——程序员“素质”提升的法宝
- LeetCode278 find the first bad version
- c语言学生通讯系统
- 哈理工 hrbust 正确的日期格式
- OkHttp3 接口回调 UI线程
- 史上最全的maven pom.xml文件教程详解
- PAT甲级1008
- 设计模式 - 工厂方法
- Unity3D游戏开发入门学习笔记
- 算法提高 格式化数据输出