Lettocde_278_first Bad Version

Source: Internet
Author: User

This article is in the study summary, welcome reprint but please specify Source: http://blog.csdn.net/pistolove/article/details/49719255



you is 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 was developed based on the previous version and all of the versions after a bad version were also bad.

suppose you have n  versions [1, 2, ..., n]  and you want to find out of the first bad one, which causes all The following ones to being bad.

Given an API which would bool isBadVersion(version) return whether is bad version . Implement a function to find the first bad version. You should minimize the number of calls to the API.


Ideas:

(1) Test instructions for a given series of version numbers, ask to find the first wrong version number. (where the current version is based on the previous version, and once a version is faulted, subsequent versions of the current version will be faulted)

(2) This topic mainly investigates the two-point search. A series of versions of 1,2,...N can be represented as Good,good,......, Good,bad, Bad, ...., just by using the binary lookup algorithm, you need to be aware that if the current version is found to be good, you need to point start to the next position in the mid.

(3) See the code below for details. I hope this article will be of some help to you.

Package Leetcode;public class First_bad_version {//Find first bad public    int firstbadversion (int n) {            int start = 0;
   
    int end = n;        while (start <=end) {    int mid = start + (end-start) >> 1;            Is bad, then look backward from the current    if (Isbadversion (mid)) {    end = mid;            } else {    start = mid + 1;        }    }        return end;        }        static Boolean isbadversion (int version) {    return true;    }}
   

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Lettocde_278_first Bad Version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.