Weird problem. Does it really matter?


  • 0
    A

    My code is like the follows, and it passed. Note the commented line. If I used that line, I got TLE exception. Is it really that important to use low +(high-low)/2 to avoid overflow in real world coding?

    public int guessNumber(int n) {
            int low = 1, high = n;
            
            while(high > low)
            {
                int mid = low + (high-low)/2;
                // int mid = (high+low)/2;
                if(guess(mid) == 0)
                    return mid;
                else if(guess(mid) == 1)
                    low = mid+1;
                else
                    high = mid-1;
            }
            return low;
        }
    

Log in to reply
 

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