LeetCode Brush Questions 461. Hamming Distance

LeetCode Brush Questions 461. Hamming Distance

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic : The
    Hamming distance between two integers refers to the number of positions where the two numbers correspond to different binary digits.
    Given two integers xand ycalculates the Hamming distance between them.
  • Example :
输入: x = 1, y = 4
输出: 2
解释:
1   (0 0 0 1)
4   (0 1 0 0)
       ↑   ↑
上面的箭头指出了对应二进制位不同的位置。
  • Tip :
    0 ≤ x, y < 231.
  • Code:
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        a = bin(x ^ y)
        return a.count('1')
# 执行用时:40 ms, 在所有 Python3 提交中击败了69.43%的用户
# 内存消耗:13.7 MB, 在所有 Python3 提交中击败了24.63%的用户
  • Algorithm Description:
    The Hamming distance is defined, obtained xand ybetween the exclusive OR, the result is converted to binary, and then the inside of the statistical 1number.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/108382241