LeetCode #461 Hamming Distance 汉明距离

461 Hamming Distance 汉明距离

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

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:

1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

The above arrows point to positions where the corresponding bits are different.

题目描述:
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 x 和 y,计算它们之间的汉明距离。

注意:
0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:

1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑

上面的箭头指出了对应二进制位不同的位置。

思路:

先用异或, 计算 1的数量可以用 num & (num - 1)来统计
时间复杂度O(1), 空间复杂度O(1)

代码:
C++:

class Solution 
{
public:
    int hammingDistance(int x, int y) 
    {
        // 也可以用 bitset类
        int result = x == y ? 0 : 1, num = x ^ y;
        while ((num = (num - 1) & num)) ++result;
        return result;
    }
};

Java:

class Solution {
    public int hammingDistance(int x, int y) {
        return Integer.toBinaryString(x ^ y).replaceAll("0", "").length();
    }
}

Python:

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        return bin(x ^ y).count("1")

你可能感兴趣的:(LeetCode #461 Hamming Distance 汉明距离)