LeetCode 477. Total Hamming Distance

477. Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Now your job is to find the total Hamming distance between all pairs of the given numbers.

Example:

<font bold>Input</font>: 4, 14, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.

Note:

  1. Elements of the given array are in the range of 0 to 10^9
  2. Length of the array will not exceed 10^4.

题目内容:
题目给出一个数组,让我们求出数组这个数组每两个数的二进制表示的汉明距离(Hamming distance)之和。例如给出数组[4, 14, 2],因为是每两个数之间的汉明距离,所以要求[4, 14], [4, 2], [14, 2]的汉明距离之和,因为这三对数字的二进制表示的汉明距离都是2,所以最终的结果是6。

解题思路:
因为题目要求我们算出每两个数之间的汉明距离,如果给出的数组很大,那么这种两个数字的组合也会有很多种,假如数组有n个数字,那么就有 n(n1)/2 那么多种组合,然后每一组数字再进行汉明距离计算,因为给出的是数字类型是int,那么一般是4个字节,也就是32位,也就是每一组数字都要遍历这32位判断是否相同。题目给出的每一个数字是在 [0,109] 之间,假如最高的位数是m,这样一看时间复杂度就是 O(n2m) 。题目给出了n的范围是 [0,104] 。所以 O(n2) 非常高的复杂度。
但是如果我们先把数字把每一位对齐来看的,以题目给出的例子数组[4, 14, 2]为例,如下图:
这里写图片描述
如果我们把每一位分开来看,其实最后的汉明距离总和可以看成是每一位的汉明距离的总和。所以可以先对每一位计算汉明距离,最后把每一位的汉明距离加起来。所以现在的问题就简化成如何计算每一位的汉明距离。
但我们单独看某一位的时候,因为二进制中每一位要么是0,要么是1,假如我们统计了所有数字中这一位的值为0的个数位n,这一位的值为1的个数为m,那么其实这一位的汉明距离就是 nm
如上图,#0表示这一位0的个数,#1表示这一位1的个数。可以看到上图中右边四位每一位的汉明距离分别为2, 2, 2, 0,除了这四位,这3个数字的其他位的值都是一样的,所以最终的汉明距离之和为6。
像上图这种情况,计算第5位及以上的位是多余的,虽然对结果没什么影响,但会影响效率。所以在程序中,我先遍历了一次数组,找出里面最大的数字,从而判断应该计算汉明距离的位数。如上述例子中最大的数字是14,那么计算 log214 并向上取整得到4,那么计算汉明距离的最高位就是第4位。这种方法的时间复杂度是 O(nm) .

代码

//
//  main.cpp
//  477. Total Hamming Distance
//
//  Created by mingjc on 2017/4/2.
//  Copyright © 2017年 mingjc. All rights reserved.
//

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

void printBin(int num) {
    int i, j;
    bool arr[100];
    i = 0;
    while(num > 0)
    {
        arr[i] = bool(num % 2);
        i++;
        num /= 2;
    }
    for(j=i-1;j>=0;j--)
        cout << arr[j];
    cout << endl;
}

class Solution {
public:
    int totalHammingDistance(vector<int>& nums) {
        int result = 0;
        int max = 0;
        vector<int>::iterator it = nums.begin();
        if (nums.begin() == nums.end()) return 0;
        while (it != nums.end()) {
            if (max < *it) {
                max = *it;
            }
            it++;
        }

        int maxBit = int(log(double(max)) / log(2.0) + 1.5);
        int* num1Count = new int[maxBit];
        int* num0Count = new int[maxBit];
        memset(num1Count, 0, maxBit * sizeof(int));
        memset(num0Count, 0, maxBit * sizeof(int));

        it = nums.begin();
        while (it != nums.end()) {
            int n = *it;
            for(int i = 0; i < maxBit; i++) {
                if ((n & 1) == 0) {
                    num0Count[i]++;
                }
                else {
                    num1Count[i]++;
                }
                n = n >> 1;
            }

            it++;
        }


        for (int i = 0; i < maxBit; i++) {
            result += num1Count[i] * num0Count[i];
        }

        return result;
    }
};

int main(int argc, const char * argv[]) {
    printBin(6);
    printBin(1);
    printBin(8);
    printBin(6);
    printBin(8);
    vector<int> nums;
    nums.push_back(6);
    nums.push_back(1);
    nums.push_back(8);
    nums.push_back(6);
    nums.push_back(8);
    Solution sln;
    cout << "-------------" << endl << sln.totalHammingDistance(nums) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值