LeetCode 278. First Bad Version

原创 2016年05月31日 14:51:57

题目说明:

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.

代码:

bool isBadVersion(int version);

class Solution {
public:
    int firstBadVersion(int n) 
    {
        int start = 1;
        int end = n;
        int mid;
        while(start <= end)
        {
            mid = start + (end - start)/2;
            if(!isBadVersion(mid))
                start = mid + 1;
            else if(isBadVersion(mid) && !isBadVersion(mid-1))
                return mid;
            else 
                end = mid - 1;
        }
        return mid;
    }
};
不能写成mid = (start + end)/2,这样会造成int溢出的现象,继而超时。

写成 mid = start +(end - start)/2即可


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

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
  • 4302

Leetcode_278_First Bad Version

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

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 (查找第一个错误版本)

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

278. First Bad Version (二分,java)

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

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

思路: 利用二分查找的思想,如果中间的是坏的,则在前半段找(包括中间这点)。否则,从[中间加一,n]这个区间找。中间点计算为了防止相加移除,采用mid = first+(last-first)/2...
  • songzige
  • songzige
  • 2016年03月29日 09:42
  • 189

LeetCode - 278. First Bad Version

这道题目的解法没有什么难度,相当于是34. Search for a Range中的寻找左侧range,但是这道题目却有一个非常隐秘却又非常大的陷阱,就是在计算mid的时候。如果我们使用mid = (...
  • shenzhu0127
  • shenzhu0127
  • 2016年07月03日 09:52
  • 180

[LeetCode]--278. First Bad Version

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • bug_moving
  • bug_moving
  • 2016年10月16日 09:41
  • 508
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode 278. First Bad Version
举报原因:
原因补充:

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