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

    278. First Bad Version

    • Total Accepted: 52704
    • Total Submissions: 228913
    • Difficulty: Easy

    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 // Forward declaration of isBadVersion API.
     2 bool isBadVersion(int version);
     3 
     4 class Solution {
     5 public:
     6     int firstBadVersion(int n) {
     7         int low=1,high=n,mid;
     8         while(low<=high){
     9             mid=low+(high-low)/2;
    10             if(isBadVersion(mid)){
    11                 high=mid-1;
    12             }
    13             else{
    14                 low=mid+1;
    15             }
    16         }
    17         return low;
    18     }
    19 };
  • 相关阅读:
    ndk学习17: jni之Java调用C&C++
    ndk学习18: JNI之C&C++调用Java
    ndk学习15: IPC机制
    正向代理和反向代理
    java内存泄露与内存溢出
    权限管理及shiro框架
    异构信息网络
    基于遗传算法的试题组卷
    Java JNI机制
    Lucene及全文搜索实现原理
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5692818.html
Copyright © 2011-2022 走看看