First Bad Version(二分查找的应用)
来源:互联网 发布:java 扫描二维码登陆 编辑:程序博客网 时间:2024/06/05 02:45
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.
mid = (low+high)/2和 mid=low+(high-low)/2 一般情况下等价,但在数比较大的时候low+high容易溢出。
算法中还有疑问的地方见代码
class Solution {public: int firstBadVersion(int n) { if(n<1) { return -1; } int low=1, high=n; int mid; while(low < high) { mid=low+(high-low)/2; if(isBadVersion(mid)==true) { high=mid;//数据结构书中二分查找是high=mid-1,此处何区别? //是因为此处下标是从1开始的吗? } else { low=mid+1; } }return low; }};
0 0
- First Bad Version(二分查找的应用)
- First Bad Version 二分查找
- <LeetCode><Easy>278First Bad Version (二分查找)
- First Bad Version 第一个坏数 二分查找
- [LeetCode] First Bad Version - 二分查找
- leetcode---First Bad Version---二分查找
- 【Leetcode】278. First Bad Version【二分查找】
- leetcode 278. First Bad Version 二分查找
- [LeetCode 278] First Bad Version(二分查找的一个常见注意点)
- LeetCode278. First Bad Version(二分)
- 278.leetcode First Bad Version(easy)[二分查找]
- leetcode_278. First Bad Version 寻找第一个坏的版本,二分查找法
- LeetCode278——First Bad Version(二分查找+一个注意点)
- 278. First Bad Version (二分,java)
- (LeetCode)First Bad Version --- 查找坏版本
- leetcode 278. First Bad Version-第一个坏版本|二分查找
- 278. First Bad Version-LeetCode(查找第一个最坏版本)
- 278. First Bad Version (查找第一个错误版本)
- 解析char *p与char p[]
- CRM-Hibernate逆向工程
- MySQL Meta中的length字段 -- (3) length的推导举例
- iOS开发中的网络请求
- XML Drawable 与9-Patches
- First Bad Version(二分查找的应用)
- FragmentPagerAdapter默认加载2项,返回时为创建视图
- centos下rpm打包简单实例
- [LeetCode]Lowest Common Ancestor of a Binary Search Tree
- thinkphp部署bae的相关配置
- 读论文
- 栈—实现数制转换
- MySQL学习笔记20150923基本概念与数据库和表的创建
- Linux系统编程获取系统的CPU资源