zoukankan      html  css  js  c++  java
  • 278. First Bad Version

    题目:

    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.

    链接: http://leetcode.com/problems/first-bad-version/

    3/6/2017

     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 lo = 0, hi = n;
     7         int mid = lo + (hi - lo) / 2;
     8 
     9         while (hi - lo > 1) {
    10             mid = lo + (hi - lo) / 2;
    11             if (isBadVersion(mid)) {
    12                 hi = mid;
    13             } else {
    14                 lo = mid;
    15             }
    16         }
    17         return hi;            
    18     }
    19 }
  • 相关阅读:
    系统安全及应用
    进程和计划任务管理
    Java技术体系
    开机十步和进程管理
    Raid
    LVM逻辑卷
    sed命令
    磁盘管理
    你的背景,是这个时代 张璁
    别将梦想停留在二十岁
  • 原文地址:https://www.cnblogs.com/panini/p/6512853.html
Copyright © 2011-2022 走看看