换壳二分
/* The guess API is defined in the parent class GuessGame.
@param num, your guess
@return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); */
public class Solution extends GuessGame {
public int guessNumber(int n) {
int hi =n;
int lo =1;
int result = 0;
while(lo<=hi)
{
int mid = lo+(hi-lo)/2;
if(guess(mid)==0)
{
result=mid;
break;
}
else if(guess(mid)==-1)
{
hi=mid-1;
}
else
lo=mid+1;
}
return result;
}
}
网友评论