// 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 { long long lb; long long hb; public: int guessNumber(int n) { lb = 1; hb = n; if (lb >= hb) { return lb; } int g = (lb + hb) / 2; int r = guess(g); while (r != 0) { if (r < 0) { hb = g - 1; } else { lb = g + 1; } if (lb >= hb) { return lb; } g = (lb + hb) / 2; r = guess(g); } return g; } };
https://leetcode.com/problems/guess-number-higher-or-lower/