[LeetCode 278] First Bad Version
2015-09-10 13:48
295 查看
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
You are given an API
Solution:
Use Binary search, get middle element, if bad version, search first half. otherwise search last half part.
are also bad.
Suppose you have
nversions
[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
versionis bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Solution:
Use Binary search, get middle element, if bad version, search first half. otherwise search last half part.
public int firstBadVersion(int n) { long begin = 1; long end = n; if(n<1) return 0; while(begin<end){ long mid = (begin+end)/2; if(isBadVersion((int)mid)){ end = mid-1; }else{ begin = mid+1; } } if(isBadVersion((int)begin)) return (int)begin; return (int)begin+1; }
相关文章推荐
- 0909编译原理
- Android drawText获取text宽度的三种方式
- Jmeter入门测试
- SDN在诺基亚5G无线计划中扮演重要角色
- Linux Purify命令
- Ubuntu 上使用 LVM 轻松调整分区并制作快照-兄弟连IT教育
- Centos6修改时区
- Leetcode: Container With Most Water
- 仿支付宝数字跳动
- 基于Guava LoadingCache实现的本地缓存工具
- 改变UITextField PlaceHolder的字体和颜色,不影响正常输入
- hdu 1018 Big Number
- Zimbra 8.5GA垃圾邮件过滤设置的5种方法
- linux 环境下安装SVN服务器
- 蓝牙学习(一)
- ORACLE EBS维护时:用户提出新需求时的处理基本步骤
- VMware中ubuntu设置成中文
- leetcode index
- web 服务器上的请求筛选被配置为拒绝该请求,因为内容长度超过配置的值
- iOS应用国际化教程(2014版)