这道题是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. 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, ...

  2. Sqlserver计算本年度工作日

    --@StartDate 本年度第一天 --@EndDate 本年度最后一天 , ) , DATEADD(yy, , , )) IF EXISTS ( SELECT * FROM tempdb..sy ...

  3. centos升级3.10内核到4.4

    为了满足k8s对内核要求,默认Centos 7的内核为3.10建议升级到4.x内核 默认3.10内核升级为4.x内核 rpm -Uvh http://www.elrepo.org/elrepo-rel ...

  4. mustache.js 数组循环的索引

    在使用mustache作为模板引擎时,想要利用数组中的对象的索引排序,却发现mustache中无法获得数组索引,在一番搜索之后,发现在数组的对象中加入索引,就可以了,示例如下 /html {{#dat ...

  5. vuejs组件

    <div id='root'> <ul> <todo-item></todo-item> </ul> </div> <sc ...

  6. Java中ArrayList的对象引用问题

    前言事件起因是由于同事使用ArrayList的带参构造方法进行ArrayList对象复制,修改新的ArrayList对象中的元素(对象)的成员变量时也会修改原ArrayList中的元素(对象)的成员变 ...

  7. 【luogu P3609 [USACO17JAN]Hoof, Paper, Scissor蹄子剪刀布】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3609 ### 看着标签什么记搜什么暴力点进来,读完题第一直觉DP? 还真是个\(DP\). 题目所描述的状态 ...

  8. C++unsigned char和char区别

    char和unsigned charchar与unsigned char都是一个字节8bit,唯一的区别是,char的最高位为符号位,因此char能表示-128~127, unsigned char( ...

  9. 解决ndk编译lua时遇到 undefined reference to '__srget'的问题

    今天用ndk r10d版本编译lua时,遇到几个错误,提示没有找到__srget 没有定义,于是看了国外的大神的解决方法, 是因为ndk在r10c之后的版本已经将getc函数屏蔽了,所以导致编译器找不 ...

  10. Vue入门之HelloWorld

    对于新学习一门技术,一门语言比如JAVA.Python等都是从编写Hello World开始的,这样一来有益于初学者的人门,并给予初学者一定的信心,所以我也从HelloWorld开始讲起. 干货: 对 ...