Java 1ms solution


  • 0
    A

    """
    public class Solution extends GuessGame {
    public int guessNumber(int n) {
    long lower = 1;
    long upper = n;
    long middle = (lower + upper) / 2;
    int x = guess((int)middle);
    boolean done = false;

        while (!done){
            if(upper == lower+1){
                middle = upper;
                x = guess((int)upper);
            }
            if(x == 1){
                lower = middle;
                middle= (lower+upper)/2;
                x = guess((int)middle);
            }
            if(x == -1){
                upper = middle;
                middle= (lower+upper)/2;
                x = guess((int)middle);
            }
            if(x == 0){
                done = true;
            }
        }
        
        return (int)middle;
    }
    

    }"""


Log in to reply
 

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