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