【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 ...
随机推荐
- Hadoop之—— WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform...
[hadoop@hadoop000 hadoop]$ ldd --version ldd (GNU libc) 2.12 Copyright (C) Free Software Foundation, ...
- Sqlserver计算本年度工作日
--@StartDate 本年度第一天 --@EndDate 本年度最后一天 , ) , DATEADD(yy, , , )) IF EXISTS ( SELECT * FROM tempdb..sy ...
- centos升级3.10内核到4.4
为了满足k8s对内核要求,默认Centos 7的内核为3.10建议升级到4.x内核 默认3.10内核升级为4.x内核 rpm -Uvh http://www.elrepo.org/elrepo-rel ...
- mustache.js 数组循环的索引
在使用mustache作为模板引擎时,想要利用数组中的对象的索引排序,却发现mustache中无法获得数组索引,在一番搜索之后,发现在数组的对象中加入索引,就可以了,示例如下 /html {{#dat ...
- vuejs组件
<div id='root'> <ul> <todo-item></todo-item> </ul> </div> <sc ...
- Java中ArrayList的对象引用问题
前言事件起因是由于同事使用ArrayList的带参构造方法进行ArrayList对象复制,修改新的ArrayList对象中的元素(对象)的成员变量时也会修改原ArrayList中的元素(对象)的成员变 ...
- 【luogu P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀布】 题解
题目链接:https://www.luogu.org/problemnew/show/P3609 ### 看着标签什么记搜什么暴力点进来,读完题第一直觉DP? 还真是个\(DP\). 题目所描述的状态 ...
- C++unsigned char和char区别
char和unsigned charchar与unsigned char都是一个字节8bit,唯一的区别是,char的最高位为符号位,因此char能表示-128~127, unsigned char( ...
- 解决ndk编译lua时遇到 undefined reference to '__srget'的问题
今天用ndk r10d版本编译lua时,遇到几个错误,提示没有找到__srget 没有定义,于是看了国外的大神的解决方法, 是因为ndk在r10c之后的版本已经将getc函数屏蔽了,所以导致编译器找不 ...
- Vue入门之HelloWorld
对于新学习一门技术,一门语言比如JAVA.Python等都是从编写Hello World开始的,这样一来有益于初学者的人门,并给予初学者一定的信心,所以我也从HelloWorld开始讲起. 干货: 对 ...