题目描述:

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

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ xy < 231.

Example:

Input: x = 1, y = 4Output: 2Explanation:
1   (0 0 0 1)
4   (0 1 0 0)↑   ↑The above arrows point to positions where the corresponding bits are different.

要完成的函数:

int hammingDistance(int x, int y)

说明:

给定两个32位的有符号型整数,当每一位不相同时,加上1,输出最后一共有多少位不相同。

代码如下:

    int hammingDistance(int x, int y) {int xx,yy,count=0;for(int i=0;i<32;i++){xx=x&1;yy=y&1;if(xx!=yy)count++;x>>=1;y>>=1;}return count;}

代码浅显易懂,实测6ms,beats 72.58% of cpp submissions。

这道题在评论区还看到了令人耳目一新的做法,采用异或,当两位相同时输出0,当两位不同时输出1。不过这种做法的时间花费实际还是跟上述代码差不多的。

代码如下:

    int hammingDistance(int x, int y) {int result=x^y,t1,count=0;for(int i=0;i<32;i++){t1=result&1;if(t1==1)count++;result>>=1;}return count;}

转载于:https://www.cnblogs.com/chenjx85/p/8948404.html

leetcode-461-Hamming Distance相关推荐

  1. Leetcode 461. Hamming Distance JAVA语言

    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(汉明距离)

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

  4. LeetCode 461. Hamming Distance

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

  5. 【LeetCode】461. Hamming Distance (java实现)

    2019独角兽企业重金招聘Python工程师标准>>> 原题链接 https://leetcode.com/problems/hamming-distance/ 原题 The Ham ...

  6. LeetCode之Hamming Distance

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

  7. 461. Hamming Distance【数学|位运算】

    2017/3/14 15:23:55 The Hamming distance between two integers is the number of positions at which the ...

  8. LeetCode之461. Hamming Distance

    ------------------------------------------------------------------ AC代码: public class Solution {publ ...

  9. LeetCode(191 461):位1的个数 Number of 1 Bits 汉明距离 Hamming Distance(Java)

    2019.9.14 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 之前在 ...

  10. C#LeetCode刷题之#461-汉明距离​​​​​​​(Hamming Distance)

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

最新文章

  1. Revit: Twinmotion工作流程学习
  2. 福利来啦!! - PostgreSQL9.5架构图与外存图
  3. 小程序 、h5图片铺满div,自适应容器大小
  4. oracle会闪,oracle闪来
  5. VMware与 Device/Credential Guard 不兼容.
  6. 某宝在售资源合集,整整1218份!
  7. JavaWeb学习方法
  8. QQ连连看外挂源码-VB源代码
  9. Google Earth Engine(GEE)统计制图
  10. html内外边距区别,css水平边距和垂直边距 css中的内边距和外边距?
  11. xlsx to vcf
  12. CFD-Post批处理(在一系列算例同一位置截取速度云图)
  13. css大图切割,利用CSS切割图片技术来动态显示图片
  14. Z39.50客户端源代码(C#)
  15. PX4 FMU启动流程 1.nsh
  16. 面试题--精选Hadoop选择题
  17. 迁移 VMware 虚拟机到 KVM
  18. Windows - 屏幕护眼的绿色
  19. 迈普路由器访问控制列表配置命令_路由与交换--ACL基本命令及其实验配置
  20. 【第3版emWin教程】第50章 emWin6.x的AppWizard使用控件经典回调方式

热门文章

  1. 关于在Mac上启动了模拟器/连接了手机之后。adb device一直提示List of devices attached的问题...
  2. 减小VirtualBox虚拟硬盘文件的大小
  3. Linux系统日志详解
  4. 电力三维基础信息平台
  5. ATT全球网络运营中心GNOC
  6. linq to sql中的自动缓存(对象跟踪)
  7. openstack在xiandian界面登录提示无效证书_注意:2019年医师资格证书开始发放啦!...
  8. python大神读取_大神教你python 读取文件并把矩阵转成numpy的两种方法
  9. 图解 Android Handler 线程消息机制
  10. Docker入门安装教程