Leetcode#461. Hamming Distance(汉明距离)
题目描述
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
输出: 2
解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
上面的箭头指出了对应二进制位不同的位置。
思路
思路一:
对两个数进行异或操作,位级表示不同的那一位为 1,统计有多少个 1 。
思路二:
使用 Integer.bitcount() 来统计 1 个的个数。
思路三:
使用 z&(z-1) 去除 z 位级表示最低的那一位。
代码实现
package BitManipulation;
/**
* 461. Hamming Distance(汉明距离)
* 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
* 给出两个整数 x 和 y,计算它们之间的汉明距离。
*/
public class Solution461 {
public static void main(String[] args) {
Solution461 solution461 = new Solution461();
int x = 1, y = 4;
System.out.println(solution461.hammingDistance(x, y));
}
/**
* 对两个数进行异或操作,位级表示不同的那一位为 1,统计有多少个 1 。
*
* @param x
* @param y
* @return
*/
public int hammingDistance(int x, int y) {
int z = x ^ y;
int cnt = 0;
while (z != 0) {
if ((z & 1) == 1) {
cnt++;
}
z = z >> 1;
}
return cnt;
}
/**
* 使用 Integer.bitcount() 来统计 1 个的个数。
*
* @param x
* @param y
* @return
*/
public int hammingDistance_2(int x, int y) {
return Integer.bitCount(x ^ y);
}
/**
* 如果一个整数不为0,那么这个整数至少有一位是1。
* 如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。
* 其余所有位将不会受到影响。
*
* @param x
* @param y
* @return
*/
public int hammingDistance_3(int x, int y) {
int z = x ^ y;
int cnt = 0;
while (z != 0) {
cnt++;
z &= (z - 1);
}
return cnt;
}
}
Leetcode#461. Hamming Distance(汉明距离)的更多相关文章
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- 4. leetcode 461. Hamming Distance
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 ...
- LeetCode 461. Hamming Distance (C++)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
随机推荐
- grep -v、-e、-E
在Linux的grep命令中如何使用OR,AND,NOT操作符呢? 其实,在grep命令中,有OR和NOT操作符的等价选项,但是并没有grep AND这种操作符.不过呢,可以使用patterns来模拟 ...
- pip install urllib3[secure] 报错 error: ffi.h: No such file or directory
解决 sudo apt-get install build-essential autoconf libtool pkg-config python-opengl python-imaging pyt ...
- Linux中断管理
CPU和外设之间的交互,或CPU通过轮询机制查询,或外设通过中断机制主动上报. 对大部分外设中断比轮询效率高,但比如网卡驱动采取轮询比中断效率高. 这里重点关注ARM+Linux组合下中断管理,从底层 ...
- WPF防止界面卡死并显示加载中效果
原文:WPF防止界面卡死并显示加载中效果 网上貌似没有完整的WPF正在加载的例子,所以自己写了一个,希望能帮到有需要的同学 前台: <Window x:Class="WpfApplic ...
- WPF: 自动设置Owner的ShowDialog 适用于MVVM
原文:WPF: 自动设置Owner的ShowDialog 适用于MVVM 原文地址:http://www.mgenware.com/blog/?p=339 WPF中的Windows的ShowDialo ...
- 相约南湖,南京都昌信息亮相南湖HIT论坛
金秋十月,雨过南湖水似油 ,烟雾蒙蒙净长空 2017年10月15日, 南湖HIT论坛迎来了第六届.本次论坛吸引了500名来自全国各地医疗机构.卫生行政主管部门的信息化主管和医疗IT企业的精英,齐聚嘉兴 ...
- xadmin后台页面的自定制(2)重写钩子函数版
由于项目有通过自定义页面来实现功能的需求,百度也查了很多资料,也没找到合适的方法,所以决定分析源码,通过对源码的分析,找到了此方法. 01-需求 首先,如果要在xadmin中展示一个数据管理页面,首先 ...
- 系统IO
系统IO:Linux系统提供给应用程序操作文件的接口 Everything is a file ,in Unix 在Unix/Linux下,万物皆文件 打开文件函数原型: #include< ...
- 一、Redis-NoSQL数据库
转载:[https://blog.csdn.net/aaronthon/article/details/81714528 ] [https://www.cnblogs.com/StanleyBlogs ...
- 精心收集的 95 个超实用的 JavaScript 代码片段( ES6+ 编写)
https://www.html.cn/archives/8748#table-of-contents https://www.haorooms.com/post/js_regexp