C#LeetCode刷题之#278-第一个错误的版本(First Bad Version)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3985 访问。
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
给定 n = 5,并且 version = 4 是第一个错误的版本。
调用 isBadVersion(3) -> false
调用 isBadVersion(5) -> true
调用 isBadVersion(4) -> true所以,4 是第一个错误的版本。
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.
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> trueThen 4 is the first bad version.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3985 访问。
public class Program {
public static void Main(string[] args) {
var n = 5;
var res = FirstBadVersion(n);
Console.WriteLine(res);
n = 5;
res = FirstBadVersion2(n);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool IsBadVersion(int version) {
return version >= 4;
}
private static int FirstBadVersion(int n) {
//LeetCode超时未AC
//暴力求解
for (var i = 0; i < n; i++) {
if (IsBadVersion(i + 1)) return i + 1;
}
return -1;
}
private static int FirstBadVersion2(int n) {
//二分求解
var low = 1;
var high = n;
while (low < high) {
//取中间索引,并不是每次取半,而是使用 (high - low) / 2 防止过界
var mid = low + (high - low) / 2;
//如果是错误的版本,直接将右指针指向 mid
//因为要找的点肯定在它左侧
if (IsBadVersion(mid)) high = mid;
//如果不是错误的版本,将左指针指向 mid + 1
//因为要找的点肯定在它右侧
else low = mid + 1;
}
return low;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3985 访问。
4
4
分析:
显而易见,FirstBadVersion 的时间复杂度为: ,FirstBadVersion2 的时间复杂度为:
。
C#LeetCode刷题之#278-第一个错误的版本(First Bad Version)的更多相关文章
- Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version)
Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version) 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版 ...
- LeetCode 278. 第一个错误的版本(First Bad Version)
278. 第一个错误的版本 LeetCode278. First Bad Version 题目描述 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每 ...
- Java实现 LeetCode 278 第一个错误的版本
278. 第一个错误的版本 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. ...
- [Java]LeetCode278. 第一个错误的版本 | First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- Leetcode刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- leetcode刷题-- 3.二分查找
二分查找 正常实现 题解 public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- 8月leetcode刷题总结
刷题链接:https://leetcode-cn.com/explore/ 根据leetcode的探索栏目,八月份一直在上面进行刷题.发现算法题真的好难,真-计算机思维. 核心是将现实问题转化为计算机 ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
随机推荐
- jquery文件表单上传
1. 引入jquery文件 <script src="js/jquery-2.1.1.min.js"></script> 2. 创建form表单,如下: ...
- vscode安装rainbow-fart(彩虹屁)插件,程序员只能自我鼓励了!!!
2020-7-10更新 Rainbow Fart 插件现以发布到 VSCode 商店,安装过 VSIX 版本的用户请卸载之前的版本,从商店安装. 从 VSCode 扩展商店 下载并安装.(更新vsco ...
- three.js 绘制3d地图
通过地图数据配合three可以做出非常酷炫的地图,在大数据展示中十分常见. 这篇郭先生就来说说使用three.js几何体制作3D地图.在线案例点击原文地址. 地图的数据是各个地图块的点数组,通过THR ...
- JQuery对下拉列表Select的一些操作
1.假如select中存在选项,需要清空的情况: $("#search").find("option").remove(); $("#search&q ...
- 完美解决pycharm 不显示代码提示问题
pycharm 不显示代码提示 1.检查IDE省电模式是否关闭状态!!! file → power save mode 取消掉 2.检查代码提示是否成功开启. setting → Inspection ...
- python txt装换成excel
工作中,我们需要经常吧一些导出的数据文件,例如sql查出来的结果装换成excel,用文件发送.这次为大家带上python装换excel的脚本 记得先安装wlwt模块,适用版本,python2-3 #c ...
- Statezhong shiyong redux props
在构造方法中使用props给state赋值不允许, 原因需要检查
- TSGCTF-web Beginner's Web (js内置方法__defineSetter__)
const fastify = require('fastify'); const nunjucks = require('nunjucks'); const crypto = require('cr ...
- nginx Dockerfile
FROM centos MAINTAINER zengxh RUN yum install -y epel-release vim pcre-devel wget net-tools gcc zlib ...
- Android TV 键盘样式开发
布局: <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=& ...