First Bad Version Leetcode java

Source: Internet
Author: User

Problem Description:

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 n has versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to B E 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 minimizethe number of calls to the API.

Analysis: give a series of version number, if one of the version number is bad, then all the version number behind it is bad. Find the first bad version. Using binary search

  Public intFirstbadversion (intN) {if(n <= 0)return0; intleft = 1,right =N,mid;  while(Left <=Right ) {Mid= left + (right-left)/2;//Prevent left + right from being too large to overflow, so transform solve           if(Isbadversion (mid)) right= Mid-1; Else Left= Mid + 1; }              returnLeft ; }

First Bad Version Leetcode java

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.