Java Solution with long


  • -1
    I
    public class Solution {
        // you need to treat n as an unsigned value
        public int hammingWeight(int n) {
            
            int count = 0;
            
            long longn = Integer.toUnsignedLong(n);
            
            while (longn > 0)
            {
                if (longn % 2 == 1)
                    count ++;
                    
                longn = longn / 2;
            }
            
            return count;
        }
    }

Log in to reply
 

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