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.

这道题让我求两个数字之间的汉明距离,题目中解释的很清楚了,两个数字之间的汉明距离就是其二进制数对应位不同的个数,那么最直接了当的做法就是按位分别取出两个数对应位上的数并异或,我们知道异或的性质上相同的为0,不同的为1,我们只要把为1的情况累加起来就是汉明距离了,参见代码如下:

解法一:

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

我们可以对上面的代码进行优化,我们可以一开始直接将两个数字异或起来,然后我们遍历异或结果的每一位,统计为1的个数,也能达到同样的效果,参见代码如下:

解法二:

class Solution {
public:
int hammingDistance(int x, int y) {
int res = , exc = x ^ y;
for (int i = ; i < ; ++i) {
res += (exc >> i) & ;
}
return res;
}
};

经过副博主@fantasywindy的提醒,上面的遍历每一位的方法并不高效,还可以进一步优化,假如数为num, num & (num - 1)可以快速地移除最右边的bit 1, 一直循环到num为0, 总的循环数就是num中bit 1的个数。参见代码如下:

解法三:

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

我们再来看一种递归的写法,非常的简洁,递归终止的条件是当两个数异或为0时,表明此时两个数完全相同,我们返回0,否则我们返回异或和对2取余加上对x/2和y/2调用递归的结果。异或和对2取余相当于检查最低位是否相同,而对x/2和y/2调用递归相当于将x和y分别向右移动一位,这样每一位都可以比较到,也能得到正确结果,参见代码如下:

解法四:

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

参考资料:

https://discuss.leetcode.com/topic/72089/java-3-line-solution

https://discuss.leetcode.com/topic/72093/java-1-line-solution-d

https://discuss.leetcode.com/topic/72289/0ms-c-two-line-solution

LeetCode All in One 题目讲解汇总(持续更新中...)

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

  1. LeetCode——Hamming Distance

    LeetCode--Hamming Distance Question The Hamming distance between two integers is the number of posit ...

  2. [LeetCode] 461. Hamming Distance 汉明距离

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

  3. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

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

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

  5. 477 Total Hamming Distance 汉明距离总和

    两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...

  6. [LeetCode] Total Hamming Distance 全部汉明距离

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

  7. [LeetCode] 477. Total Hamming Distance 全部汉明距离

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

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

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

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

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

随机推荐

  1. wxWidgets

    wxWidgets Code::Blocks环境 Code::Blocks下载: Code::Blocks使用: codeblocks-16.01mingw-setup.exe 它的gcc版本为4.9 ...

  2. .NET Core之Entity Framework Core 你如何创建 DbContext

    本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明博客园蜗牛原文地址 http://www.cnblogs.com/tdws/p/5874212.html. 目前国内各大论坛,各位大牛的分 ...

  3. python学习笔记1:python入门

    关于版本的选择 按照网上的说法,如果python是为了在工作中使用,选择2.7版本的.这里我选择2.7.9版本的来进行学习: Python是什么? 是一种高级的计算机程序设计语言.应用范围比较广,go ...

  4. [示例] Firemonkey TGridLayout & TGridPanelLayout 布局

    说明:使用 TGridLayout & TGridPanelLayout 来布局 源码下载:[示例]TestGridPanelLayout_布局_20161223.zip 展示:

  5. HashMap与HashTable的区别

    HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到Collection框架以及多线程的面试,可以说是不完整.而Collection框架的问题不涉及到HashSet和H ...

  6. Springl利用Aspectj的扩展实现Aop

    1. Spring为什么要使用Aspectj Spring Aop:Spring自己原生的Aop,只能用一个词来形容:难用. 你需要实现大量的接口,继承大量的类,所以spring aop一度被千夫所指 ...

  7. 数据结构:栈 顺序表方法和单链表方法(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- class StackUnderflow(ValueError): pass #链表节点 class Node ...

  8. php静态缓存简单制作

    制作缓存的目的是为了让我们的页面运行更加快速,减少读取数据库内容的次数,给用户更好的体验,为此我们可以使自己的程序做一下缓存,并且设置一个缓存过期的时间,来保证与数据库的一致,当然并不是所有的程序都适 ...

  9. 浏览器渲染引擎,提高css渲染速度。

    一.渲染引擎渲染引擎的职责是……渲染,也就是把请求的内容显示到浏览器屏幕上.默认情况下渲染引擎可以显示HTML,XML文档以及图片. 通过插件(浏览器扩展)它可以显示其它类型文档. 二.各种渲染引擎我 ...

  10. 小程序https Android 安卓可以发request请求,IOS 苹果 发请求失败问题

    如果一个机器可以发送成功,一个机器发送失败,那多半是是域名的https支持的问题 那就用腾讯云的这个ssl测试工具检测下 https://www.qcloud.com/product/ssl#user ...