汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
 
输出: 2
 
解释:
1 (0 0 0 1)
4 (0 1 0 0)
        ↑   ↑
 
上面的箭头指出了对应二进制位不同的位置。
 
 
^
如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。

思路:利用了异或的性质^   两个数字的二进制字符,比如   01001^10010=11011

异或^得到的结果中含有多少个1,就表示他的汉明距离。和上一道题:位1的个数 类似https://www.cnblogs.com/patatoforsyj/p/9475383.html

代码如下:

class Solution {
public int hammingDistance(int x, int y) {
int cnt = 0;
x=x^y;
while(x!=0)
{
if((x&1)==1)
cnt++;
x=x>>1;
}
return cnt;
}
}

leetcode-汉明距离的更多相关文章

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

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

  2. [LeetCode] Hamming Distance 汉明距离

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

  3. LeetCode 461 汉明距离/LintCode 365 统计二进制中1的个数

    LeetCode 461. 汉明距离 or LintCode 365. 二进制中有多少个1 题目一:LeetCode 461. 汉明距离 LeetCode 461.明距离(Hamming Distan ...

  4. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

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

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

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

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

  7. LeetCode 461. Hamming Distance (汉明距离)

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

  8. leetCode:461 汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 思路: 当看到"对应二进制位不同的位置的数目"这 ...

  9. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

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

  10. Leetcode#461. Hamming Distance(汉明距离)

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

随机推荐

  1. java Thread 类 run 和 start 方法区别

    public class ThreadModle { public static void main(String[] args) throws InterruptedException { Thre ...

  2. VS 2017 + EF6 + MySQL5.7 建立实体模型闪退问题

    具体环境是:VS2017 ..NET Framework 4.6 .MySql.Data.Entity 6.9.12 在这个环境下总是不成功,具体是在这一步闪退,也不报错: 在点击“下一步”后,没有进 ...

  3. linux mysql命令行查看显示中文

    linux 命令行查看mysql的库字符集是utf8,查询某个表时,仍然是显示不了中文, 之后使用了命令 mysql>set  names utf8;就可以正常显示中文了. 如何才更好的使mys ...

  4. Java中集合随笔

    先上一张图:关于collection接口的 一.Collection中的常用功能: boolean add(Object e): 向集合中添加元素void clear():清空集合中所有元素boole ...

  5. CSS权重的比较方法

    CSS的权重如下: !important  Infinity正无穷 行间样式  1000 id     100 class|属性|唯类 10 标签|伪元素  1 通配符   0 256进制 当出现多个 ...

  6. iOS之estimatedHeightForRowAtIndexPath避免程序EXC_BAD_ACCESS

    在你的项目中把estimatedHeightForRowAtIndexPath方法写实现以下,返回一个估计高度(随便估,笔者建议还是按照正常思路来写,大概高度是多少就返回多少),这样就不会报EXC_B ...

  7. Qt5应用程序封包

    系统环境:windows10+vs2017+qt5.12 目的:生成.exe可执行文件. 步骤: 1.选择release模式,生成解决方案. 2.打开命令行,cd到生成的可执行文件.exe目录下 3. ...

  8. chromium之ScopedNSAutoreleasePool浅析

    上代码,看看注释 ScopedNSAutoreleasePool只有Mac系统特有的,也可以理解为OC特有的函数, 其他系统为空实现   // On the Mac, ScopedNSAutorele ...

  9. 支付宝PC端的支付(PHP)

    现在的一个项目支付宝PC支付还是用的以前网关:https://mapi.alipay.com/gateway.do  (是支付宝 2012年7月13日的3.3版本) 所以现在要做下支付宝PC支付的升级 ...

  10. python网络编程之进程

    一.什么是进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础.在早期面向进程设计的计算机结构中,进程是程序的基本执行实 ...