【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题。
题目描述:
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数
x和y,计算它们之间的汉明距离。注意:
0 ≤x,y< 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)的更多相关文章
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode Total Hamming Distance
原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- 4. leetcode 461. Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode 461 Hamming Distance 解题报告
题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...
随机推荐
- java cpu使用率高异常排查
1.top命令对cpu进行排序shift+p 2.pwdx pid查找业务进程路径 3.top -Hp pid查看相关负载线程pid 4.printf “0x%x\n” 线程pid // 将线 ...
- 弹出页面第一次加载可以生成table和方法的绑定,第二次点击进来不能生成table和方法的帮定
问题原因: 弹出页面的写法是每次点击都会在原有页面基础之上新添加一个将其覆盖,原有页面不关闭.我用的生成table和点击事件的绑定是id选择器.页面中只绑定第一次的页面,第二次的页面作用不上. 解决: ...
- python_30_购物车复习
prodcut_list=[ ('Iphone', 5800), ('Mac Pro', 9800), ('Bike', 800), ('Watch', 10600), ('Coffee', 31), ...
- python_29_三级菜单
menu={ '北京':{ '海淀':{ '五道口':{ '搜狐':{}, '网易':{}, 'Google':{}, }, '中关村':{ '爱奇艺':{}, '汽车之家':{}, '优酷':{}, ...
- react树状组件
最近在react项目中需要一个树状组件,但是又不想因为这个去引入一套UI组件,故自己封装了一个基于react的树状组件, 个人认为比较难得部分在于数据的处理,话不多说直接上代码: 下面是tree.js ...
- Java程序设计第四次作业内容 第五次作业10月9号发布,为第三章全部例题
第六题:使用判断语句,根据数字,输出对应的中文是星期几? 直接使用一个if语句的情况 int weekDay=3; if(weekDay==1){ sop("今天是星期一"); } ...
- SAP 各模块常用的BAPI
MM模块 1. BAPI_MATERIAL_SAVEDATA 创建物料主数据 注意参数EXTENSIONIN的使用,可以创建自定义字段 例如:WA_BAPI_TE_MARA-MATERIAL = IT ...
- python 基础 for else
for one in many_list: if "k" in one: print "在里面" break else: print "没有在里面&q ...
- win10下安装mysql-5.7.23-winx64
Step1 官方下载地址 https://dev.mysql.com/downloads/mysql/ 选择手动下载版本 解压到自己指定的路径 上图中的my.ini及data文件夹在压缩包里是没有的, ...
- flock文件锁
linux中的定时任务crontab会定时执行一些脚本,但是脚本的时间往往无法控制,当脚本的执行时间过长,可能会导致上一次任务的脚本还没执行完,下一次任务的脚本又开始执行的问题.这种情况下会出现一些并 ...