Why it is wrong with input 789012340567 ?(Accepted JAVA answer !)


  • 0
    N

    Problem "Number of 1 Bits ", My solution is :

       public class Solution {
            // you need to treat n as an unsigned value
             public static long hammingWeight(long n) {
        		   //get the last 32 bit
        		 	n = n & 0xFFFFFFFFL;
        	        int num = 0;
        	        while(n>0){
        	            int lastBitNum = (int) (n&0x1);
        	            num += lastBitNum ;
        	            n = n>>1;
        	        }
        	        return num;
        	    }
        	 
        }
    

    accoring to the Hamming weight ,this solution will be wrong with input 789012340567 ,why ?


Log in to reply
 

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