This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The isBadVersion API is defined in the parent class VersionControl. | |
boolean isBadVersion(int version); */ | |
public class Solution extends VersionControl { | |
public int firstBadVersion(int n) { | |
if (n < 2) { | |
return n; | |
} | |
// core logic | |
int start = 1; | |
int end = n; | |
while(start + 1 < end) { | |
int mid = (end - start) / 2 + start; | |
if (isBadVersion(mid)) { | |
end = mid; | |
} else { | |
start = mid; | |
} | |
} | |
if (isBadVersion(start)) { | |
return start; | |
} else { | |
return end; | |
} | |
} | |
} |
No comments:
Post a Comment