Java Simple Solution


  • 0
    D
    public class Solution extends GuessGame {
        public int guessNumber(int n) {
            return guessNumber(1, n);
        }
        
        private int guessNumber(int low, int high) {
            int numberToGuess = low + (high - low) / 2;
            int guessResponse = guess(numberToGuess);
            
            if (guessResponse == 0) {
                return numberToGuess;
            } else if (guessResponse == -1) {
                return guessNumber(low, numberToGuess - 1);
            } else {
                return guessNumber(numberToGuess + 1, high);
            }
            
        }
    }
    

Log in to reply
 

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