其他分享
首页 > 其他分享> > 每日一题:477. 汉明距离总和

每日一题:477. 汉明距离总和

作者:互联网

解题思路

逐位统计即可

代码

class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.bitCount(x^y);
    }
    public int totalHammingDistance(int[] nums) {
        int n = nums.length,ans = 0;
        for (int i=0;i<n-1;i++){
            for (int j=i+1;j<n;j++){
                ans+=hammingDistance(nums[i],nums[j]);
            }
        }
        return ans;
    }
}

标签:hammingDistance,return,nums,int,汉明,477,ans,public,总和
来源: https://blog.csdn.net/weixin_45221477/article/details/117359721