Leetcode_278_first Bad Version

Source: Internet
Author: User

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



You are 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 of the version is developed based on the previous version and all of the versions after a bad version are bad.

Suppose you have n versions [1, 2, ..., n] and you want to find out the "the" one, which causes all the following ones To being bad.

You are are given an API bool Isbadversion (version) which would return whether version are bad. Implement a function to find the "the" You are should minimize the number of calls to the API.


Ideas:

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

(2) This question mainly examines the two-point search. 1,2,...N a series of versions can be expressed as Good,good,......, Good,bad, Bad, ..., only through the binary lookup algorithm to judge, it should be noted that if the current version is found to be good, you need to point to the next location in 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 the 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 find
    		if (Isbadversion (mid) {End
    			= Mid
    			
    			
    		} from the current backward 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.