leetcode-461-Hamming Distance
题目描述:
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 ≤ x
, y
< 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相关推荐
- Leetcode 461. Hamming Distance JAVA语言
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(汉明距离)
Q:The Hamming distance between two integers is the number of positions at which the corresponding bi ...
- LeetCode 461. Hamming Distance
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 【LeetCode】461. Hamming Distance (java实现)
2019独角兽企业重金招聘Python工程师标准>>> 原题链接 https://leetcode.com/problems/hamming-distance/ 原题 The Ham ...
- LeetCode之Hamming Distance
1.题目 The Hamming distance between two integers is the number of positions at which the corresponding ...
- 461. Hamming Distance【数学|位运算】
2017/3/14 15:23:55 The Hamming distance between two integers is the number of positions at which the ...
- LeetCode之461. Hamming Distance
------------------------------------------------------------------ AC代码: public class Solution {publ ...
- LeetCode(191 461):位1的个数 Number of 1 Bits 汉明距离 Hamming Distance(Java)
2019.9.14 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) github:https://github.com/ChopinXBP/LeetCode-Babel 之前在 ...
- C#LeetCode刷题之#461-汉明距离(Hamming Distance)
问题 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意:0 ≤ x, y < 231. 输入: x = 1, y = ...
最新文章
- Revit: Twinmotion工作流程学习
- 福利来啦!! - PostgreSQL9.5架构图与外存图
- 小程序 、h5图片铺满div,自适应容器大小
- oracle会闪,oracle闪来
- VMware与 Device/Credential Guard 不兼容.
- 某宝在售资源合集,整整1218份!
- JavaWeb学习方法
- QQ连连看外挂源码-VB源代码
- Google Earth Engine(GEE)统计制图
- html内外边距区别,css水平边距和垂直边距 css中的内边距和外边距?
- xlsx to vcf
- CFD-Post批处理(在一系列算例同一位置截取速度云图)
- css大图切割,利用CSS切割图片技术来动态显示图片
- Z39.50客户端源代码(C#)
- PX4 FMU启动流程 1.nsh
- 面试题--精选Hadoop选择题
- 迁移 VMware 虚拟机到 KVM
- Windows - 屏幕护眼的绿色
- 迈普路由器访问控制列表配置命令_路由与交换--ACL基本命令及其实验配置
- 【第3版emWin教程】第50章 emWin6.x的AppWizard使用控件经典回调方式
热门文章
- 关于在Mac上启动了模拟器/连接了手机之后。adb device一直提示List of devices attached的问题...
- 减小VirtualBox虚拟硬盘文件的大小
- Linux系统日志详解
- 电力三维基础信息平台
- ATT全球网络运营中心GNOC
- linq to sql中的自动缓存(对象跟踪)
- openstack在xiandian界面登录提示无效证书_注意:2019年医师资格证书开始发放啦!...
- python大神读取_大神教你python 读取文件并把矩阵转成numpy的两种方法
- 图解 Android Handler 线程消息机制
- Docker入门安装教程