C++ 0ms, use lower+(upper-lower)/2 instead of (lower+upper)/2


  • 0
    int guessNumber(int n) {
            int lower=0;
            int upper=n;
            while(guess(lower+(upper-lower)/2)!=0){
                if(guess(lower)==0) return lower;
                if(guess(upper)==0) return upper;
                if(guess(lower+(upper-lower)/2)<0)
                upper=lower+(upper-lower)/2;
                else
                lower=lower+(upper-lower)/2;
            }
            return lower+(upper-lower)/2;
        }
    

Log in to reply
 

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