Why the test case 2147483648 tells me the answer is wrong


  • 0
    T

    Below is my code. I have tested with eclipse since the maximum of int in eclipse is 2147483647. All the answers within that range are correct. But when I test it with leetcode, it says the output is 0 rather than 1. I am very confused. What is wrong?

    '''
    public class hamming_distance {

        // you need to treat n as an unsigned value
        public static int hammingWeight(int n) {
            int tag=0;
            while (n/2!=0){
             if (n%2==1)
             tag++;
              n=n>>1;
             
             
             }
             if (n==1)
             tag++;
             return tag;
        }
        public static void main(String[] args) {
    		int c;
        	c=hammingWeight(2147483647);
    		System.out.print(c);
    		// TODO Auto-generated method stub
    
    	}
    }
    

    '''


  • 0
    T

    @tjuli I donot find the place that I can edit the original post.
    I use the unsigned right shift,
    n=n>>>1;
    now the test case 4294967295 shows the output is 0, which is expected 32.
    I am very confused.


Log in to reply
 

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