【Leetcode】278. First Bad Version【二分查找】

原创 2016年08月28日 22:17:52

278. First Bad Version

  • Total Accepted: 58296
  • Total Submissions: 248778
  • 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.

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question


思路:二分

1、注意返回条件begin 和 end相等,和一开始调用findRange的条件。

2、这个是找第一个坏版本,所以递归调用的时候isBadVersion(mid)为1时,调用findRange(begin,mid),即不把mid排除,保留0和1的交汇点。

3、mid = (low+high)先发生了数据溢出,应该将其改为mid = low+(high-low)/2      //不知道这么解释对不对。


代码:

public class Solution extends VersionControl {
    public int firstBadVersion(int n) {
        if(n == 1||n == 0) return n;
        int res = findRange(1,n);
        return res;
    }
    
    public int findRange(int begin,int end){
        if(begin == end) return begin;
        
        int mid = begin + (end - begin)/2;
        boolean checkRes = isBadVersion(mid);

        if(checkRes)  
          return findRange(begin,mid);
        else 
          return findRange(mid+1,end);
    }
}


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

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

Leetcode_278_First Bad Version

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

278. First Bad Version (二分,java)

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

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

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

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

leetcode 278. First Bad Version 二分查找

You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
  • JackZhang_123
  • JackZhang_123
  • 2017年09月27日 10:13
  • 51

278. First Bad Version LeetCode

题意:寻找第一个坏的版本。坏版本之后的都是坏版本。 题解:二分。// Forward declaration of isBadVersion API. bool isBadVersion(int v...
  • youthinkwu
  • youthinkwu
  • 2016年02月13日 16:00
  • 101

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

leetcode 解题记录, 278. First Bad Version
  • vinson0526
  • vinson0526
  • 2016年05月24日 16:20
  • 330
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【Leetcode】278. First Bad Version【二分查找】
举报原因:
原因补充:

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