First Bad Version

Source: Internet
Author: User

The code base version is a integer start from 1 to N. One day, someone committed a bad version in the code case, so it caused this version and the following versions is all FA iled in the unit tests. Find the first bad version.

Determine which version is the first bad one isBadVersion . The details interface can be found in the code ' s annotation part.

Notice

Please read the annotation in code area to get the correct-on-the-isbadversion in different language. For example, Java isSVNRepo.isBadVersion(v)

Example

Given n = 5 :

isBadVersion(3) -> falseisBadVersion(5) -> trueisBadVersion(4) -> true

Here we were 100% sure that the 4th version was the first bad version.

Analysis:

Two-point lookup.

1 /**2 * public class Svnrepo {3 * public static Boolean isbadversion (int k);4  * }5 * can use Svnrepo.isbadversion (k) to judge whether6 * The KTH code version is bad or not.7 */8 classSolution {9     /**Ten * @param n:an integers. One * @return: An integer which are the first bad version. A      */ -      Public intFindfirstbadversion (intN) { -         intStart =1; the         intEnd =N; -          -          while(Start <=end) { -             intMid = (start + end)/2; +             if(Svnrepo.isbadversion (mid) = =false) { -Start = mid +1; +}Else { AEnd = Mid-1; at             } -         } -          -         returnstart; -     } -}

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.