[JAVA] Check with individual bit / T : O(N), S : O(1)


  • 0
    J
    class Solution {
        
        public int totalHammingDistance(int[] nums) {
            int sum = 0;
            for(int i = 0; i < 32; i++){
                int count = 0;
                for(int j = 0; j < nums.length; j++){
                    count += (nums[j] >> i) & 0x1;
                }
                sum += count * (nums.length - count);
            }
            
            return sum;
        }
    }
    

Log in to reply
 

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