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 }
  • 相关阅读:
    团队作业六
    团队作业五
    团队作业(四)
    团队作业(三)
    团队作业二
    宇宙圣斗士队介绍
    团队作业之七
    团队作业之六
    团队作业五
    奥特曼小分队之四(Work Breakdown Structure)
  • 原文地址:https://www.cnblogs.com/panini/p/6512853.html
Copyright © 2011-2022 走看看