这道题是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. centos6.5_64bit-nginx开机自启动

    Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动. 首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:   ...

  2. World Wind Java开发之八——加载本地缓存文件构建大范围三维场景(

    http://blog.csdn.net/giser_whu/article/details/42044599 上一篇博客主要是针对小文件直接导入WW中显示,然而当文件特别大时,这种方式就不太可行.因 ...

  3. 在Spark集群中,集群的节点个数、RDD分区个数、​cpu内核个数三者与并行度的关系

    梳理一下Spark中关于并发度涉及的几个概念File,Block,Split,Task,Partition,RDD以及节点数.Executor数.core数目的关系. 输入可能以多个文件的形式存储在H ...

  4. WQS二分学习笔记

    前言 \(WQS\)二分听起来是个很难的算法,其实学起来也并不是那么难. 适用范围 在某些题目中,会对于某个取得越多越优的物品,限定你最多选择\(k\)个,问你能得到的最优答案. 例如这道题目:[CF ...

  5. LigerUI的下拉框行和树的设置(表单生成)

    http://blog.csdn.net/dxnn520/article/details/8194767 // ---------------------- // [下拉树设置 -- 单选] {dis ...

  6. CSVDE

    csvde -f C:\export_OrganizationalUnit.csv -r '(objectClass=organizationalUnit)' -l 'displayName,prox ...

  7. AFN post的数据编码格式问题

    想到写任何关于AFN的东西其实我是拒绝的,因为自己这也是第一次用,毕竟AFN现在是最为流行的网络框架了,害怕自己理解的有误,所以不敢造次! 先在这里大致讲解一下过程吧,后期发现了再更正(主要是想让看官 ...

  8. vue项目跨域问题

    跨域 了解同源政策:所谓"同源"指的是"三个相同". 协议相同 域名相同 端口相同 解决跨域 jsonp 缺点:只能get请求 ,需要修改B网站的代码 cors ...

  9. PHP数据库扩展 - PDO操作

    PDO操作 PDO操作 描述:odp是php对数据库操作统一化的操作 语法:$pdo = new PDO("DB名:host=主机名;dbname=DB名","DB账号& ...

  10. 2.在Cisco Packet Tracer里交换机默认网关的配置(实现跨网段telnet)

    我们将在此拓扑图的基础上进行实验 大多命令都可用tab键位来补齐 1.分别给pc机设置好ip地址 pc2为:192.168.1.1 pc3为:192.168.2.1 两台计算机处在不同的网段之中 2. ...