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 ≤ xy < 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.

两个数字之间的汉明距离是其二进制数对应位不同的个数,两个数异或,把为1的数量累加起来就是汉明距离。

Java:

public int hammingDistance(int x, int y) {
int res = x ^ y;
int count = 0;
for (int i = 0; i < 32; i++) {
if ((res & 1) != 0)
count++;
res >>= 1;
}
return count;
}   

Python:

class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
distance = 0
z = x ^ y
while z:
distance += 1
z &= z - 1
return distance

Python:

class Solution(object):
def hammingDistance(self, x, y):
"""
:type x: int
:type y: int
:rtype: int
"""
return bin(x ^ y).count('1') 

C++:

class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0;
for (int i = 0; i < 32; ++i) {
if ((x & (1 << i)) ^ (y & (1 << i))) {
++res;
}
}
return res;
}
};

C++:  

class Solution {
public:
int hammingDistance(int x, int y) {
int res = 0, exc = x ^ y;
while (exc) {
++res;
exc &= (exc - 1);
}
return res;
}
};

C++:

class Solution {
public:
int hammingDistance(int x, int y) {
if ((x ^ y) == 0) return 0;
return (x ^ y) % 2 + hammingDistance(x / 2, y / 2);
}
};

  

类似题目:  

[LeetCode] 191. Number of 1 Bits 二进制数1的个数

All LeetCode Questions List 题目汇总

[LeetCode] 461. Hamming Distance 汉明距离的更多相关文章

  1. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  2. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  3. LeetCode 461. Hamming Distance (汉明距离)

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

  4. [LeetCode] 461. Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  5. 4. leetcode 461. Hamming Distance

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

  6. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  7. LeetCode 461. Hamming Distance (C++)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  8. 461. Hamming Distance(汉明距离)

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

  9. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

随机推荐

  1. Tomcat项目内存参数调优

    一.常见的Java内存溢出有以下三种: 1. Java.lang.OutOfMemoryError: Java heap space 即JVM Heap溢出 解释说明:JVM在启动的时候会自动设置JV ...

  2. zookeeper 的 docker 镜像使用

    dockerhub 网址:https://hub.docker.com/_/zookeeper

  3. 10、Python迭代器与生成器(iterator、for循环、generator、yield)

    一.迭代器(foreach) 1.可迭代的对象 内置有__iter__方法的都叫可迭代的对象. Python内置str.list.tuple.dict.set.file都是可迭代对象. x = 1._ ...

  4. python写入csv

    import xlwtimport csvnewfile=open("wu.csv","w",newline="")filewriter=c ...

  5. MAT022 Foundations of Statistics

    MAT022 Foundations of Statistics and Data Science Summative Assessment 2019/20MAT022 Foundations of ...

  6. kubectl kubernetes cheatsheet

    from : https://cheatsheet.dennyzhang.com/cheatsheet-kubernetes-a4 PDF Link: cheatsheet-kubernetes-A4 ...

  7. 持续集成学习10 Pipline初探

    一.流水线概述 1.案例 2.流水线语法(input 处会阻塞住让你选择) 3.执行脚本 4.查看语法

  8. C# VS 调试 动态加载的 DLL

    原文:https://www.cnblogs.com/DasonKwok/p/10510218.html 在这篇文章的底部,有提供示例的Demo,可以参考一下哦,拿来直接就可以运行. 说明: 编译类库 ...

  9. REdis主从复制之repl_backlog

    目录 目录 1 1. 前言 1 2. 配置项 1 3. redisServer 2 4. feedReplicationBacklog-写repl_backlog 3 5. addReplyRepli ...

  10. nodejs之express生成项目[windows平台]

    安装nvm,nvm下载地址   用于管理多个版本node,此处可省略! 安装nodejs,nodejs下载地址    淘宝镜像 安装cnpm命令,后面包可以使用cnpm命令安装,此处可省略,如果安装了 ...