278. First Bad Version
2016-02-21 13:23
302 查看
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
bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
Solution:
运行速度:快
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:
// Forward declaration of isBadVersion API. bool isBadVersion(int version); class Solution { public: int firstBadVersion(int n) { int left = 1; int right = n; while(isBadVersion(right)){ if(isBadVersion(left)) return left; int mid = left + (right-left)/2; if(isBadVersion(mid)){ right = mid-1; } else{ left = mid+1; } } return right+1; } };心得:主要是分治的思想,自己写的比较繁琐,虽然运行时间不长;
运行速度:快
相关文章推荐
- 【慕课笔记】第二章 认识JAVA中的字符串 第4节 JAVA中String类的常用方法(二)
- scala中的面向对象定义类,构造函数,继承
- python,django做中间件屏蔽非法访问
- 链表初始化总结
- juce教程004-模态对话框
- 【收藏】Android AutoLayout全新的适配方式, 堪称适配终结者
- 把安卓源代码中的system app独立出来,像开发普通app那样开发
- 通读SDWebImage①--总体梳理、下载和缓存
- 常用的排序算法
- javascript+HTML5自定义元素播放焦点图动画
- C语言实现的高精度计算器
- hdu 1068 girls and boys(二分图求最大独立集)
- 学习笔记:极客学院-2. Unity3D 基础操作-Scene与场景漫游
- 组织对项目的影响
- [ahk]获取网站的源代码
- [深度好文]电动车真的很环保吗?
- 函数参数传递2
- Apple Pay 应用 demo --备用哦
- 《Android源码设计模式》读书笔记 (10) 第10章 解释器模式
- CLH自旋锁