题目

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.

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

分析

题目要求在产品的n个版本中找到第一个bad version。在所有版本中,若是当前i版本是bad,则后续i+1版本也是bad。

考察二分查找的应用。

AC代码

// Forward declaration of isBadVersion API.
bool isBadVersion(int version); class Solution {
public:
int firstBadVersion(int n) {
if (n < 1)
return -1;
int lhs = 1, rhs = n; while (lhs < rhs)
{
int mid = lhs + (rhs - lhs) / 2;
//找到bad version
if (isBadVersion(mid))
{
rhs = mid;
}
//mid并不是bad version 则first bad肯定在右边
else{
lhs = mid + 1;
}
}//while
return lhs;
}
};

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

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. LeetCode(122) Best Time to Buy and Sell Stock II

    题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. LeetCode(113) Path Sum II

    题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...

  7. LeetCode(107) Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  8. LeetCode(4)Median of Two Sorted Arrays

    题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. JavasJavaScript笔试题

    这几天参加了好多场宣讲会,记录一下遇到的编程题还有平时练习遇到的(如果有更好的方法欢迎提出,本人也是菜鸟一枚): 1.数组查重,并返回出重复次数最多的项并记录重复次数: function chacho ...

  2. 076 Minimum Window Substring 最小窗口子字符串

    给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...

  3. 一文读懂DDD

    何为DDD DDD不是架构设计方法,不能把每个设计细节具象化,DDD是一套体系,决定了其开放性,体系中可以用任何一种方法来解决这些问题,但是如果一些关键问题没有具体方案落地,可能让团队无所适从. 有的 ...

  4. 漫谈Code Review的错误实践

    从刚开始工作时到现在,已经写了7年的代码,大部分代码都被人review过,自己也review了很多人的代码.在上一家公司的时候,我负责的一轮面试是专门进行Code Review的练习和经验谈. 通过在 ...

  5. redis启动内存不足

    redis-server.exe redis.windows.conf  --maxheap 2gb

  6. 《C#高效编程》读书笔记01-使用属性而不是可访问的数据成员

    在需求变更中,属性比数据成员更容易修改,例:客户对象不该与空白名称,若你使用公有属性封装Name,那么现在修改一处,而数据成员则可能要修改多处 public class Customer { priv ...

  7. 公司项目git开发流程规范

    手动修改冲突之后,git add . git commit ,git push

  8. javaSe-反射2

    //创建实体类 package com.java.chap07.sec03; public class Student { private String name; private Integer a ...

  9. jsoup获取网页属性

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  10. BZOJ 2654: tree Kruskal+二分答案

    2654: tree Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 1863  Solved: 736[Submit][Status][Discuss ...