Binary search C++ solution


  • 0
    A

    Binary search C++ solution

    class Solution {
    public:
        int guessNumber(int n) {
            int low = 1, hi = n;
            while (low < hi) {
                int mid = low + (hi - low) / 2;
                int g = guess(mid);
                if (g == -1)
                    hi = mid - 1;
                else if (g == 1)
                    low = mid + 1;
                else
                    return mid;
            }
            return low;
        }
    };
    

Log in to reply
 

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