java binary search


  • 0
    L
     public int guessNumber(int n) {
    
       int i = 1, j = n;
        while(i < j) {
        
        int mid =i+(j-i)/2;
        
        if(guess(mid) == 0) {
            return mid;
        } else if(guess(mid) == 1) {
            i = mid + 1;
        } else {
            j = mid-1;
        }
        
    }
        return i;
    }

Log in to reply
 

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