题目:
解答:
1 /** 2 * Forward declaration of guess API. 3 * @param num your guess 4 * @return -1 if num is lower than the guess number 5 * 1 if num is higher than the guess number 6 * otherwise return 0 7 * int guess(int num); 8 */ 9 10 class Solution { 11 public: 12 int guessNumber(int n) 13 { 14 int low = 1; 15 int high = n; 16 while (low <= high) 17 { 18 int mid = low + (high - low) / 2; 19 int res = guess(mid); 20 if (res == 0) 21 return mid; 22 else if (res < 0) 23 high = mid - 1; 24 else 25 low = mid + 1; 26 } 27 return -1; 28 } 29 };
原文:https://www.cnblogs.com/ocpc/p/12830441.html