Code runs correctly in eclipse, but got error when tested by leetcode


  • 0
    X

    Very confused. Much obliged if anyone can help!
    Fail on the test case:

    Input: 2147483648 (10000000000000000000000000000000)
    Output: 4294967295 (11111111111111111111111111111111)
    Expected: 1 (00000000000000000000000000000001)

    public static int reverseBits(long n) { 
            long length = 31, base = 1;
            long ans = 0;
            while (n != 0) {
                ans += n / ((long)1 << length) * base; 
                n = n % ((long)1 << length);
                length--;
                base <<= 1; 
            };  
            return (int)ans; 
        }

Log in to reply
 

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