Share My Straightforward Java Solution


  • 1
    S
    public class Solution {
        public int totalHammingDistance(int[] nums) {
            int n = nums.length;
            int sum = 0;
            int[][] pair = new int[32][2];
            for (int num : nums) {
                int k = 0;
                while (k < 32) {
                    int cur = (num & 1);
                    if (cur == 0) {
                        sum += pair[k][1];
                    } else {
                        sum += pair[k][0];
                    }
                    pair[k][cur]++;
                    k++;
                    num >>= 1;
                }
            }
            return sum;
        }
    }
    

  • 1
    J
    This post is deleted!

Log in to reply
 

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