278. First Bad Version
阿新 • • 發佈:2018-04-06
star ems nds source uri class create 負數 java
原題鏈接:https://leetcode.com/problems/first-bad-version/description/``
實現如下:
/**
* Created by clearbug on 2018/4/6.
*/
public class VersionControlSolution extends VersionControl {
public int firstBadVersion(int n) {
int start = 1;
int end = n;
while (start <= end) {
// int medium = (start + end) / 2; // 不要使用這種方法,當 start 和 end 都接近 int 類型最大值時會導致溢出,以致 medium 可能成為負數
int medium = start + (end - start) / 2;
if (isBadVersion(medium)) {
end = medium - 1;
} else {
start = medium + 1;
}
}
return start;
}
}
278. First Bad Version