leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II

374. Guess Number Higher or Lower

二分查找就好

// 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:
int guessNumber(int n) {
int start = , end = n;
while(start < end){
int mid = start + (end - start)/;
int res = guess(mid);
if(res == )
return mid;
else if(res < )
end = mid - ;
else
start = mid + ;
}
return start;
}
};
上一篇:[LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II


下一篇:Android——旋转屏幕导致Activity重建解决方法