Just Eazy Binary Search

  • 0
    // 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 {
        int guessNumber(int n) {
            int left = 1,right=n;
                int mid = left+(right-left)/2;
                int res = guess(mid);
                if(res==1)  left = mid+1;
                else if(res==-1)    right = mid-1;
                else    return mid;
            return right;

Log in to reply

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