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 ...
随机推荐
- R语言学习——数组
> #数组(array)与矩阵类似,但维度可大于2.可通过array函数构建,形式如下:myarray<-array(vector,dimensions,dimnames)> #其中 ...
- python中的闭包和装饰器
重新学习完了函数,是时候将其中的一些重点重新捋一捋了,本次总结的东西只有闭包和装饰器 1.闭包 闭包是python函数中的一个比较重要功能,一般闭包都是用在装饰器上,一般学完闭包就会去学习装饰器,这俩 ...
- copy 和 deepcopy的区别
import copy a = [1, 2, 3, 4, ['a', 'b']] b = a # 引用,除非直接给a重新赋值,否则a变则b变,b变则a变 c = copy.copy(a) # 浅复制, ...
- 相片后期处理,PS调出温暖的逆光美女
原图: 效果图: 后面就是开PS导图: 说明下,因为拍的时候大概知道自己的方法会让照片变暖,现场光线又很暖,所以色温要调低一些,这边是4100,其他不用变,直接转JPG调色了 1:第一步是加第一个曲线 ...
- 根据json生成c#实体类
vs 编辑->选择性粘贴->将json粘贴为类
- [转帖]golang操作mysql使用总结
golang操作mysql使用总结 https://www.cnblogs.com/hanyouchun/ 讲解的很详细~ 前言 Golang 提供了database/sql包用于对SQL数据库的访问 ...
- kettle集群(转换)
1.定义子服务器 新建子服务器中有一个必须为主服务器 新建集群 在需求集群运行的步骤中右键集群进行使用
- pc端前端和手機端區別
1.pc端寬度比較固定,手機端可以橫屏或者豎屏: 2.pc端不需要處理手機觸摸,而手機端需要: 3.pc端不需要處理鍵盤事件: 3.pc的瀏覽器內核很多,手機端基本上是webkit或者是基於webki ...
- 放弃幻想,全面拥抱Transformer:自然语言三大特征抽取器CNN/RNN/Transformer比较
参考: https://zhuanlan.zhihu.com/p/54743941
- LDOOP ADD_PRINT_TEXT多页项
纯文本打印(ADD_PRINT_TEXT)项超过宽度且高度不够的情况下,不会隐藏后面的内容,而是会分到下一页.分页数量和每页显示内容多少 和设置的纯文本打印项高度有关.LODOP.SET_PRINT_ ...