zoukankan      html  css  js  c++  java
  • LeetCode 278.First Bad Version(E)(P)

    题目:

    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调用次数,不能用顺序查找,使用二分法。

    代码:

     1 /* The isBadVersion API is defined in the parent class VersionControl.
     2       boolean isBadVersion(int version); */
     3 
     4 public class Solution extends VersionControl {
     5     public int firstBadVersion(int n) {
     6         int start = 1,end =n,mid=1;
     7         while(start + 1 <end){
     8             mid = start + (end - start)/2;
     9             if(isBadVersion(mid)){
    10                 end = mid;
    11             }else{
    12                 start = mid;
    13             }
    14         }
    15         if(isBadVersion(start)){
    16             return start;
    17         }
    18         return end;
    19     }
    20 }
  • 相关阅读:
    [bzoj 2151]种树(贪心)
    [bzoj 1026]windy数(数位DP)
    [BZOJ2038]小Z的袜子(莫队算法)
    spark调优
    从底层谈WebGIS 原理设计与实现(一):开篇
    css兼容性记录
    H5新标签
    AMD 与CMD
    rem 响应 js函数
    SVN服务器搭建和使用(一)
  • 原文地址:https://www.cnblogs.com/melbourne1102/p/6647176.html
Copyright © 2011-2022 走看看