374. 猜数字大小
思路:二分法
/**
* Forward declaration of guess API.
* @param num your guess
* @return -1 if num is lower than the guess number
* 1 if num is higher than the guess number
* otherwise return 0
* int guess(int num);
*/
class Solution {
public:
int guessNumber(int n) {
int low = 0,high = n;
while(low<=high){
int middle = low + (high-low)/2;
int flag = guess(middle);
if(flag==0) return middle;
else if(flag== 1) low = middle+1;
else if(flag== -1)high = middle-1;
}
return high;
}
};