从0开始的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 = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
题目大意:
给你两个数字,返回这两个数字的汉明距离,汉明距离就是看两个数字的二进制代码,按位异或,比如1和4,二进制分别为(0001)和(0100),一个个异或比较后得到(0101)因为第二位和第四位不一样。然后数一下得到的数有几个1.这里有两个1,所以返回2
解法:
这是一个简单的题目,先定义一个计数器和一个记录两个数字异或的变量。即int cnt=0;int e=x^y;
然后数一下e的二进制代码里面有几个1,我的做法是让它与1按位与,如果和1按位与是1,那么就说明当前最后一位是1,如果不是,那当前最后一位为0.然后再左移,直到它变为0
比如说1和4,按位与后得到的e==5(二进制为0101)
1)与1按位与,得到1,则cnt+1;
2)右移一位,得到(010),与1按位与后得到0,则cnt不改变;
3)右移,得到(01),与1按位与得到1,cnt+1;
4)右移,得到(0),与1按位与得到0,cnt不变;
5)此时e==0,退出循环,返回cnt的值,即2.
代码:
int hammingDistance(int x, int y) {
,e=x^y;
while(e){
)
++r;
e>>=;
}
return r;
}
从0开始的LeetCode生活—461-Hamming Distance(汉明距离)的更多相关文章
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode之461. Hamming Distance
------------------------------------------------------------------ AC代码: public class Solution { pub ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- 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
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- 异常-----freemarker.core.ParseException: Encountered "string"
1.错误描述 freemarker.core.ParseException: Encountered "string" at line 21, column 21 in type. ...
- java继承属性相关介绍
这个只需要记住一点,父类的任何属性(变量可以看做属性),子类均可继承并覆盖,allType(father)-->changeAnyType(son)-->AnyType 这是父类的所有代表 ...
- 【BZOJ4555】求和(第二类斯特林数,组合数学,NTT)
[BZOJ4555]求和(第二类斯特林数,组合数学,NTT) 题面 BZOJ 题解 推推柿子 \[\sum_{i=0}^n\sum_{j=0}^iS(i,j)·j!·2^j\] \[=\sum_{i= ...
- 【BZOJ1257】余数之和(数论分块,暴力)
[BZOJ1257]余数之和(数论分块,暴力) 题解 Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + - + k mod n的 ...
- 图文详解AO打印(标准模式)
一.概述 AO打印是英文Active-Online Print的简称,也称主动在线打印.打印前支持AO通讯协议的AO打印机(购买地址>>)首先通过普通网络与C-Lodop服务保持在线链 ...
- ajax 上传文件
最近做公司官网,需要用到上传文件功能,由于是用JQ写的,用到了input标签 的type=file 属性,然后利用表单提交方式上传,代码如下: $('#upload_video').change(fu ...
- SVN提示图标详解
常见SVN图标的含义 灰色向右箭头:本地修改过 蓝色向左箭头:SVN上修改过 灰色向右且中间有个加号的箭头:本地比SVN上多出的文件 蓝色向左且中间有个加号的箭头:SVN上比本地多出的文件 灰色向右 ...
- linux同步与通信
这几天读完了UNP v2,对进程间通信与同步的方式有所了解,现对主要的知识点总结如下: 根据出现的历史,先有的管道,FIFO,信号,然后是systemV IPC,再是后来的Poxis IPC,syst ...
- Java 对IP请求进行限流.
高并发系统下, 有三把利器 缓存 降级 限流. 缓存: 将常用数据缓存起来, 减少数据库或者磁盘IO 降级: 保护核心系统, 降低非核心业务请求响应 限流: 在某一个时间窗口内对请求进行限速, 保护系 ...
- centos7 升级 git(2.14.3) 版本
下载 wget https://www.kernel.org/pub/software/scm/git/git-2.14.3.tar.gz 安装依赖包 yum install curl-devel ...