Simple java binary search solution


  • 0
    V
    public int guessNumber(int n) {
        int low = 1;
        int high = n;
        while (low <= high) {
            int mid = low + (high-low)/2;
            int g = guess(mid);
            if (g == 0)
                return mid;
            else if (g < 0)
                high = mid-1;
            else
                low = mid+1;
        }
        throw new IllegalArgumentException();
    }

Log in to reply
 

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