Lettocde_278_First Bad Version

Source: Internet
Author: User

Lettocde_278_First Bad Version

 

 

 


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 havenVersions[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 APIbool isBadVersion(version)Which will return whetherversionIs bad. Implement a function to find the first bad version. You shoshould minimize the number of cballs to the API.

 

Ideas:

(1) If a series of version numbers are specified, you must find the version number of the first error. (The current version is based on the previous version. If an error occurs in a specific version, subsequent versions of the current version will fail)

(2) This question mainly describes binary search. You can set 1, 2 ,... n series of versions are represented as good, good ,......, good, bad, bad ,....., you only need to use the Binary Search Algorithm for determination. Note that if you find that the current version is good, you need to point start to the next position of the mid.

(3) For details, see the code below. I hope this article will help you.

 

Package leetcode; public class First_Bad_Version {// find the first bad public int firstBadVersion (int n) {int start = 0; int end = n; while (start <= end) {int mid = start + (end-start)> 1; // if it is bad, find if (isBadVersion (mid) {end = mid ;} else {start = mid + 1 ;}} return end;} static boolean isBadVersion (int version) {return true ;}}

 

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.