leetcode 477. Total Hamming Distance

Orignal question can be found here.

My solution is as follows:

int totalHammingDistance(int* nums, int numsSize)
{
  int result = 0;
  int num_of_1 = 0;
  for(int i=1; i<31; ++i)
    {
      num_of_1 = 0;
      for(int j=0; j<numsSize; ++j)
        {
          if(nums[j]%2 == 1)
            ++ num_of_1;
          nums[j] /= 2;
        }
      result += num_of_1 * (numsSize-num_of_1);
    }
  return result;
}

Leave a Reply

Your email address will not be published. Required fields are marked *