Guess
原创
©著作权归作者所有:来自51CTO博客作者mb62c788fd198da的原创作品,请联系作者获取转载授权,否则将追究法律责任
solution:
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num);
class Solution {
public:
//注意的是guess函数到底是谁的数字。
int guessNumber(int n) {
//if(guess(n)==0) return n;//err.
int left = 1, right = n, mid;
while(left<=right)
{
mid = left + (right-left)*0.5;
int t = guess(mid);
if(t==1) left = mid+1;
else if(t== -1) right = mid-1;
else return mid;
}
return left;
}
};