题目:

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.

思路:

1.查找第一个,要求减少API调用次数,不能用顺序查找,使用二分法。

代码:

 /* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */ public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1,end =n,mid=1;
while(start + 1 <end){
mid = start + (end - start)/2;
if(isBadVersion(mid)){
end = mid;
}else{
start = mid;
}
}
if(isBadVersion(start)){
return start;
}
return end;
}
}

LeetCode 278.First Bad Version(E)(P)的更多相关文章

  1. C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介

    目录 为什么要刷LeetCode 刷LeetCode有哪些好处? LeetCode vs 传统的 OJ LeetCode刷题时的心态建设 C#如何刷遍LeetCode 选项1: VS本地Debug + ...

  2. C#刷遍Leetcode面试题系列连载(2): No.38 - 报数

    目录 前言 题目描述 相关话题 相似题目 解题思路: 运行结果: 代码要点: 参考资料: 文末彩蛋 前言 前文传送门: C# 刷遍 Leetcode 面试题系列连载(1) - 入门与工具简介 上篇文章 ...

  3. C#刷遍Leetcode面试题系列连载(4) No.633 - 平方数之和

    上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetCode 中的另一道数学题吧~ 今天要给大家分析的面试题是 LeetCode 上第 633 号问题, Leetcode 633 - 平方数 ...

  4. C#刷遍Leetcode面试题系列连载(5):No.593 - 有效的正方形

    上一篇 LeetCode 面试题中,我们分析了一道难度为 Easy 的数学题 - 自除数,提供了两种方法.今天我们来分析一道难度为 Medium 的面试题. 今天要给大家分析的面试题是 LeetCod ...

  5. C# 刷遍 Leetcode 面试题系列连载(3): No.728 - 自除数

    前文传送门: C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介 C#刷遍Leetcode面试题系列连载(2): No.38 - 报数 系列教程索引 传送门:https://enjoy2 ...

  6. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

  7. LeetCode解题记录(贪心算法)(二)

    1. 前言 由于后面还有很多题型要写,贪心算法目前可能就到此为止了,上一篇博客的地址为 LeetCode解题记录(贪心算法)(一) 下面正式开始我们的刷题之旅 2. 贪心 763. 划分字母区间(中等 ...

  8. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

  9. Saving James Bond - Easy Version (MOOC)

    06-图2 Saving James Bond - Easy Version (25 分) This time let us consider the situation in the movie & ...

随机推荐

  1. 吴裕雄--天生自然C语言开发:排序算法

    #include <stdio.h> void bubble_sort(int arr[], int len) { int i, j, temp; ; i < len - ; i++ ...

  2. iOS商品详情、ffmpeg播放器、指示器集锦、自定义圆弧菜单、实用工具等源码

    iOS精选源码 电商商品详情 Swift.两种方式实现tableViewCell拖拽功能 ffmpeg+openGL播放器 微信聊天表情雨.表情下落.表情动画 iOS指示器集锦 弹窗增加 picker ...

  3. K3CLOUD表关联

    销售订单关联发货通知单 销售订单表 T_SAL_ORDER A T_SAL_ORDERENTRY B T_SAL_ORDERENTRY_LK C 发货通知单表 T_SAL_DELIVERYNOTICE ...

  4. numpy矩阵运算--矩阵乘法

    1)元素对应相乘,使用 multiply 函数或 * 运算符来实现 a = np.array([2,2,2])b = np.array([3,3,3]) c1 = a*a c1 array([4, 4 ...

  5. 106)PHP,缩略图代码和结果展示

    首先是 代码展示: <?php class CImage { /** * 生成保持原图纵横比的缩略图,支持.png .jpg .gif * 缩略图类型统一为.png格式 *@param $src ...

  6. EncryptUtils

    package me.zhengjie.core.utils; import org.springframework.util.DigestUtils; import javax.crypto.Cip ...

  7. html,将元素水平,垂直居中的四种方式

    将元素垂直,水平居中分两种情况:一个是元素尺寸固定,二是元素尺寸不固定 一.尺寸固定 方法1:定位 ,50%,margin负距 .box{ width: 400px; height: 300px; b ...

  8. https协议 和 Charles 进行https抓包原理

    本文转载自:https://blog.csdn.net/fox64194167/article/details/80387696 1.对称加密 其变成复杂的加密密文发送出去.收信方收到密文后,若想解读 ...

  9. 最优化算法——常见优化算法分类及总结

    之前做特征选择,实现过基于群智能算法进行最优化的搜索,看过一些群智能优化算法的论文,在此做一下总结. 在生活或者工作中存在各种各样的最优化问题,比如每个企业和个人都要考虑的一个问题"在一定成 ...

  10. chap1-HttpRequest测试类

    # HttpRequest测试类, 封装请求方法 import requests class HttpRequest: def http_request(self, url, method, data ...