/* 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 index, low = 1, high = n-1;
while(low<=high){
index = low + ((high-low)>>1);
if(!isBadVersion(index)&& isBadVersion(index+1))
return index + 1;
else if(isBadVersion(index))
high = index - 1;
else
low = index + 1;
}
return 1;
}
}
/* 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 index, low = 1, high = n;
while(low<high){
index = low + ((high-low)>>1);
if(isBadVersion(index))
high = index;
else
low = index + 1;
}
return high; //low可以
}
}
public int firstBadVersion(int n) {
int index, low = 1, high = n;
while(low<=high){
index = low + ((high-low)>>1);
if(isBadVersion(index))
high = index-1;
else
low = index + 1;
}
return low; //只能是low此时 low位置为true, hight为false
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/Y_peak/article/details/120264116
内容来源于网络,如有侵权,请联系作者删除!