这道题是LeetCode里的第461道题。

题目描述:

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

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

注意:

0 ≤ xy < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ 上面的箭头指出了对应二进制位不同的位置。

题目都暗示了使用位运算,然后把结果转换成二进制数。

解题代码:

class Solution {
public int hammingDistance(int x, int y) {
int c,res = 0;
c = x ^ y;//异或位运算 while(c != 0) {
if(c % 2 == 1) {//二进制转换
res++;
}
c/=2;
} return res;
}
}

提交结果:

个人总结:

普及一下汉明码吧:汉明的基本操作是异或运算。汉明码可以用来检测转移数据是发生的错误,并修正错误。如:QQ上有人给你发了条消息 "0 1111",其中汉明码为 "0" 在开头,发送中途信号被干扰了,变成了 "1 1110",其中汉明码为 "1" 在开头由 "0" 变成了 "1",这个时候汉明码就起作用了,但是这只是仅仅知道了错误,但却不能纠错,想要纠错只能再添加一位汉明码。具体的东西还是太深奥了,感兴趣想了解的自行百度

【LeetCode】汉明距离(Hamming Distance)的更多相关文章

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

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

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

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

  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(汉明距离)

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

  5. [Swift]LeetCode461. 汉明距离 | Hamming Distance

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

  6. LeetCode Total Hamming Distance

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

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

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

  8. LeetCode:461. Hamming Distance

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

  9. 4. leetcode 461. Hamming Distance

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

  10. LeetCode 461 Hamming Distance 解题报告

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

随机推荐

  1. sql server 2012安装程序图

    重点:下面的安装步骤都在断网环境下安装的 因为我想查看联网跟没有联网SQL2012会不会下载并安装.net2.0 和.net3.5和.net4和SP1补丁包 我的环境: 没有集成SP1补丁包的安装包大 ...

  2. Samuraiwtf-的确是很好的渗透学习平台

    有人问我要渗透测试平台学习,我想到了Samurai ,记得里面带有很多的,这里来推广一下. Samurai Web 测试框架很多人说是live CD测试环境,但是现在似乎不是了,至少目前最新版的只有这 ...

  3. POJ 1742 Coins(多重背包,优化)

    <挑战程序设计竞赛>上DP的一道习题. 很裸的多重背包.下面对比一下方法,倍增,优化定义,单调队列. 一开始我写的倍增,把C[i]分解成小于C[i]的2^x和一个余数r. dp[i][j] ...

  4. 设置RichTextBox控件的文本的对齐方式

    实现效果: 知识运用: RichTextBox控件的SelectionAlignment属性 //获取或设置在当前选择或插入点的对齐方式 public HorizontalAlignment Sele ...

  5. 2017.12.17 servlet 生命周期

    servlet生命周期一般分为4个: 加载----实例化------服务-----销毁 (1)加载: 加载一般是在运行tomcat容器时来完成,将servlet类加载到tomcat中,或者是客户端发来 ...

  6. Drupal7新装一个主题时页面白屏,如何设置一个默认主题?

    问题: 请问我不小心退出登陆了 但这个主题没有登录口 而且之前不知道为什么我其他界面都不能显示内容所以 ?q=user 也不行,怎么办呢?看网上说更换默认主题 去variable表里把默认主题换了,我 ...

  7. 箭头函数 -------JavaScript

    本文摘要:http://www.liaoxuefeng.com/ ES6标准新增了一种新的函数:Arrow Function(箭头函数). 为什么叫Arrow Function?因为它的定义用的就是一 ...

  8. GPT分区表的备份与恢复

    GPT分区表的备份与恢复  keenshoes 2016-01-13 21:02:25 关键词: GPT, Partition, MBR,APPLE, GUID, Protective MBR 对于现 ...

  9. 如何在vue项目中使用md5加密

    npm安装: npm install --save js-md5 1.在需要使用的项目文件中引入: import md5 from 'js-md5'; 使用: md5('hello world') / ...

  10. mysql的性能优化案例

    在一次项目实现中,以前写了个程序,将在txt文件中的电话号码和对应的类型往数据库中插入,小数据量的情况下,用个数组遍历循环的方式,很容易解决,但是当数据量一下 但是,几十万个电话一次性插入,就变得耗时 ...