[leetcode][Binary Search] First Bad Version
来源:互联网 发布:质量好的冬装淘宝店 编辑:程序博客网 时间:2024/05/17 09:00
题目:
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.
// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: int firstBadVersion(int n) { if(n <= 0) return -1; int low = 1, high = n; while(low <= high){ int mid = low + (high - low)/2; if(isBadVersion(mid)) { if(mid == 1 || !isBadVersion(mid-1)) return mid; high = mid-1; } else low = mid + 1; } }};
0 0
- [leetcode][Binary Search] First Bad Version
- leetcode :Binary Search:First Bad Version(278)
- LeetCode-Easy部分中标签为 Binary Search 278. First Bad Version
- Binary search(1) -- First Bad Version, Arranging Coins
- [LC][array][Binary Search] 278. 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
- LeetCode || 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
- zzuli OJ 1096: 水仙花数(函数专题)
- java的(PO,VO,TO,BO,DAO,POJO)类名包名解释
- 备忘小算法:Java将一维数组数据绘制成N行M列矩阵(如九宫格)
- SelectIndex、SelectValue、SelectText、SelectItem
- 第二周项目1—C/C++语言中函数参数传递的三种方式
- [leetcode][Binary Search] First Bad Version
- 剑指Offer系列---(18)反转链表(递归与非递归实现)
- 注明的div+img 的3px bug
- 第2周项目1 C/C++语言中函数参数传递的三种方式
- 线性表--顺序表(自建顺序表算法库)
- ImageSpan的使用
- IP地址,子网掩码,默认网关,DNS服务器详解
- 短信验证码自动填写
- 第二周项目3-程序的多文件组织