278. First Bad Version

原创 2016年06月01日 15:06:37

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.
Analysis:
注意有可能第一个就是坏版本。

Source Code(C++):

#include <iostream>
#include <vector>
#include <cmath>
#include <string>
using namespace std;

// Forward declaration of isBadVersion API.
bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) {
        if (n<2){
            return 1;
        }
        if (isBadVersion(1)){
            return 1;
        }
        if (!isBadVersion(n)){  //全为好版本
            return 0;
        }
        int head=1, tail=n; //认为head是好,tail是坏版本
        while(tail-head>1) {
            int mid=(tail-head)/2+head;
            if (isBadVersion(mid)){
                tail=mid;
            }
            else {
                head=mid;
            }
        }
        return tail;
    }
};

int main() {
    Solution sol;
    cout << sol.firstBadVersion(1);
    cout << sol.firstBadVersion(2);
    cout << sol.firstBadVersion(10);
    return 0;
}

bool isBadVersion(int version) {
    if (version>1){
        return true;
    }
    else {
        return false;
    }
}
版权声明:转载请注明出处,谢谢。

Leetcode_278_First Bad Version

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/49719255 You are a pr...
  • pistolove
  • pistolove
  • 2015年11月08日 16:22
  • 1494

leetcode 278: First Bad Version

First Bad Version Total Accepted: 1415 Total Submissions: 5826 You are a product manag...
  • xudli
  • xudli
  • 2015年09月08日 08:49
  • 4309

LeetCode278——First Bad Version(二分查找+一个注意点)

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • booirror
  • booirror
  • 2015年09月09日 09:44
  • 4811

278. First Bad Version (查找第一个错误版本)

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • xiangwanpeng
  • xiangwanpeng
  • 2016年10月28日 10:32
  • 207

leetcode 278. First Bad Version-第一个坏版本|二分查找

原题链接:278. First Bad Version 【思路-Java】 本题是二分法的应用,要注意①处的细节,如果直接 int mid = (right + left) / 2可能会产生溢出: p...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016年05月19日 21:49
  • 270

278. First Bad Version (二分,java)

278. First Bad Version DescriptionSubmissionsSolutions Total Accepted: 90906Total Submi...
  • CriminalCode
  • CriminalCode
  • 2017年04月03日 10:07
  • 161

278. First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • u013828950
  • u013828950
  • 2016年01月06日 00:18
  • 119

278. First Bad Version*

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • alwaystry
  • alwaystry
  • 2017年01月09日 20:35
  • 80

leetcode:278. First Bad Version

278. First Bad Version You are a product manager and currently leading a team to develop a new pr...
  • dumpling5232
  • dumpling5232
  • 2016年08月24日 16:10
  • 71

LeetCode - 278. First Bad Version

这道题目的解法没有什么难度,相当于是34. Search for a Range中的寻找左侧range,但是这道题目却有一个非常隐秘却又非常大的陷阱,就是在计算mid的时候。如果我们使用mid = (...
  • shenzhu0127
  • shenzhu0127
  • 2016年07月03日 09:52
  • 180
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:278. First Bad Version
举报原因:
原因补充:

(最多只允许输入30个字)