Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Created May 28, 2018 14:50
Show Gist options
  • Save yitonghe00/7953e7a02057d23ecf74a02bd4232c6c to your computer and use it in GitHub Desktop.
Save yitonghe00/7953e7a02057d23ecf74a02bd4232c6c to your computer and use it in GitHub Desktop.
278. First Bad Version
/* The isBadVersion API is defined in the parent class VersionControl.
boolean isBadVersion(int version); */
public class Solution extends VersionControl {
public int firstBadVersion(int n) {
int begin = 0, end = n, mid;
while(begin < end) {
mid = begin + (end - begin) / 2;
if(isBadVersion(mid)) {
end = mid; //Ensure there is at least one bad version in the range. Also end will move at least one step.
} else {
begin = mid + 1; //begin will move at least one step.
}
}
//start == end == mid
return start;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment