share my C++ solution


  • 0
    N
    // 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 u = 0;
            while (n != 1){
                int mid = (n - 1) / 2 + 1;
                int x = guess(mid + u);
                if (x == 0) return mid + u;
                if (x == -1) n = mid - 1;
                if (x == 1){
                    n = n - mid;
                    u = u + mid;
                }
            }
            return u + 1;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.