题目描述

两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。

给出两个整数 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(汉明距离)的更多相关文章

  1. [LeetCode] 461. Hamming Distance 汉明距离

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

  2. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  3. 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(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  5. 4. leetcode 461. Hamming Distance

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

  6. LeetCode 461 Hamming Distance 解题报告

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

  7. LeetCode 461. Hamming Distance (C++)

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

  8. 461. Hamming Distance(汉明距离)

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

  9. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

随机推荐

  1. CentOS 7 安装Kubernetes(单机版)

    一.关闭CentOS自带的防火墙服务 #  systemctl disable firewalld # systemctl  stop firewalld 二.安装etcd和Kubernetes软件( ...

  2. Linux内存管理 (13)回收页面

    专题:Linux内存管理专题 关键词:LRU.活跃/不活跃-文件缓存/匿名页面.Refault Distance. 页面回收.或者回收页面也即page reclaim,依赖于LRU链表对页面进行分类: ...

  3. python2.7添加注释后,代码无法保存

    最近需要学习一下Python,然后开始学习中,使用的编辑环境是Python自带的IDLE Python的注释使用的是   # 然后我再代码添加注释 #Python的注释是这个字符 发现始终无法保存代码 ...

  4. Kafka 详解(二)------集群搭建

    这里通过 VMware ,我们安装了三台虚拟机,用来搭建 kafka集群,虚拟机网络地址如下: hostname                      ipaddress             ...

  5. FineUI十周年纪念版即将发布(基于像素的响应式布局,独此一家)!

    [新版预报]FineUI十周年纪念版(v5.0.0)即将于2018-04-23发布! 官网示例已更新:http://pro.fineui.com/ 特别助攻:基于像素的响应式布局,FineUI独家秘笈 ...

  6. Linux 字符编码 查看与转换

    Linux 查看文件编码格式 Vim 查看文件编码 set fileencoding // 即可显示文件编码格式 若想解决Vim查看文件乱码问题, 可以在 .vimrc 文件添加 set encodi ...

  7. 细述:nginx http内核模块提供的变量和解释

    导读 ngx_http_core_module模块在处理请求时,会有大量的变量,这些变量可以通过访问日志来记录下来,也可以用于其它nginx模块. 在我们对请求做策略如改写等等都会使用到一些变量,顺便 ...

  8. 播放器更改语言归属地后Cnario player软件无法启动的问题

    打开系统运行,输入regedit,进入注册表编辑器. 找到HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\C-nario\Player下面culture 的值,删除即可 ...

  9. 深入Eureka/Feign/Hystrix原理学习(1)

    第一步: 创建注册中心项目,引入cloud discovery相关依赖. ①在pom文件中引入相关依赖. ②在启动类上加上@EnableEurekaServer注解,标注这是一个注 册中心. ③在ap ...

  10. 返回通知 对方法返回的结果可以进行加工 例如请求接口后 返回的json参数可以加工成对象返回给调用者