【leetcode】278. First Bad Version
problem
solution1:遍历;
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int ver = ;
while(ver<n)
{
if(isBadVersion(ver)) return ver;
ver++;
}
return ver;
}
};
solution2:二分法;
// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
int left = , right = n, mid;
while(left<right)
{
mid = left + (right-left)/;
if(isBadVersion(mid)) right = mid;
else left = mid + ;//err.
}
return left;
}
};
参考
1. Leetcode_278_First Bad Version;
完
【leetcode】278. First Bad Version的更多相关文章
- 【LeetCode】278. First Bad Version 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...
- 【easy】278. First Bad Version
有一系列产品,从某个开始其后都不合格,给定一个判断是否合格的函数,找出N个产品中第一个不合格的产品. 正确答案: // Forward declaration of isBadVersion API. ...
- 【LeetCode】165. Compare Version Numbers 解题报告(Python)
[LeetCode]165. Compare Version Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- 如何仅用递归函数和栈操作逆序一个栈——你要先用stack实现,再去改成递归——需要对递归理解很深刻才能写出来
/** * 如何仅用递归函数和栈操作逆序一个栈 * 题目: * 一个栈依次压入1,2,3,4,5,那么从栈顶到栈底分别为5,4,3,2,1. * 将这个栈转置后,从栈顶到栈底为1,2,3,4,5,也就 ...
- 微信订阅号,获取用户openid
在微信后台,启用服务器配置. 服务器URL地址,要通过Token的验证. private void Auth() { string token = ConfigurationManager.AppSe ...
- android library打包成aar形式供别的项目引用
1.我们项目已经有library存在,我们有需求是需要把library供其他项目引用,而且不能让其他项目随意更改我们项目的代码. 2.Rebuild Project 后zxinglib生成aar文件, ...
- vim 文本会在末尾自动添加换行 md5文件和数据只不对应
在linux系统 vim md5data # 打开文件 写入 abc 保存 md5sum md5Data 的计算值和openssl代码计算值不一样 原因在于linux文本文件末尾自动添加了换行 解 ...
- js如何比较两个日期之间相差数(天、时、分、秒)
首先,我们模拟一个例子 引入js文件 <script type="text/javascript" src="jquery.min.js">< ...
- nginx在linux上的安装与配置详解(一)
Nginx的安装与配置详解 (1)nginx简介 nginx概念: Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like ...
- 1014 2018 使用FLAG counter
AmazingCounters.com 不能用了,服务器访问不了了,之前的博客浏览量也看不到了,1014号换了个新的--FLAG counter,就是博客右边那个.
- mvc+struct1+struct2
转一篇写得很棒的文章:https://my.oschina.net/win199176/blog/208171?p=7&temp=1495894148424 1.基于web开发中最原始的jsp ...
- iOS 统一配置
1 .统一设置nav标题样式: - (void)_setNavigationTitle { NSDictionary *navigationParams = @{NSForegroundColorAt ...
- 1005 继续(3n+1)猜想
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对 n=3 进行验证的时 ...