其他分享
首页 > 其他分享> > 477. 汉明距离总和

477. 汉明距离总和

作者:互联网

 

链接:477. 汉明距离总和

题解:

 

 

class Solution {
public:
    int totalHammingDistance(vector<int>& nums) {
        int ans = 0;
        for (int i = 0; i <= 31; ++i) {
            int zero_cnt = 0;
            int un_zero_cnt = 0;
            for (auto num : nums) {
                if ((num >> i)&1) {
                    ++un_zero_cnt;
                } else {
                    ++zero_cnt;
                }
            }
            ans += zero_cnt * un_zero_cnt;
        }
        return ans;
    }
};

 

标签:cnt,int,ans,++,zero,477,汉明,总和
来源: https://blog.csdn.net/INGNIGHT/article/details/117376420