comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
中等 |
|
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量。
给你一个整数数组 nums
,请你计算并返回 nums
中任意两个数之间 汉明距离的总和 。
示例 1:
输入:nums = [4,14,2] 输出:6 解释:在二进制表示中,4 表示为 0100 ,14 表示为 1110 ,2表示为 0010 。(这样表示是为了体现后四位之间关系) 所以答案为: HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6
示例 2:
输入:nums = [4,14,4] 输出:4
提示:
1 <= nums.length <= 104
0 <= nums[i] <= 109
- 给定输入的对应答案符合 32-bit 整数范围
我们在
时间复杂度
class Solution:
def totalHammingDistance(self, nums: List[int]) -> int:
ans, n = 0, len(nums)
for i in range(32):
a = sum(x >> i & 1 for x in nums)
b = n - a
ans += a * b
return ans
class Solution {
public int totalHammingDistance(int[] nums) {
int ans = 0, n = nums.length;
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += (x >> i & 1);
}
int b = n - a;
ans += a * b;
}
return ans;
}
}
class Solution {
public:
int totalHammingDistance(vector<int>& nums) {
int ans = 0, n = nums.size();
for (int i = 0; i < 32; ++i) {
int a = 0;
for (int x : nums) {
a += x >> i & 1;
}
int b = n - a;
ans += a * b;
}
return ans;
}
};
func totalHammingDistance(nums []int) (ans int) {
for i := 0; i < 32; i++ {
a := 0
for _, x := range nums {
a += x >> i & 1
}
b := len(nums) - a
ans += a * b
}
return
}
function totalHammingDistance(nums: number[]): number {
let ans = 0;
for (let i = 0; i < 32; ++i) {
const a = nums.filter(x => (x >> i) & 1).length;
const b = nums.length - a;
ans += a * b;
}
return ans;
}
impl Solution {
pub fn total_hamming_distance(nums: Vec<i32>) -> i32 {
let mut ans = 0;
for i in 0..32 {
let mut a = 0;
for &x in nums.iter() {
a += (x >> i) & 1;
}
let b = (nums.len() as i32) - a;
ans += a * b;
}
ans
}
}