1ms Java Easy Solution(no binary tree,no integer overflow)


  • 0
    F
    public class Solution extends GuessGame {
        public int guessNumber(int n){
            if(guess(1) ==0)
                return 1;
            if(guess(n) == 0)
                return n;
            int lower = 1;
            int upper = n;
            int current = (1 + n) >> 1;
            while(guess(current) != 0){
                if(guess(current) == 1)
                    lower = current;
                if(guess(current) == -1)
                    upper = current;
                current = lower % 2 != 0 || upper % 2 != 0 ?  (lower >> 1) + (upper >> 1) + 1  : (lower >> 1) + (upper >> 1);
            }
    
            return current;
        }
    }
    

Log in to reply
 

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