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 ...
随机推荐
- VS 附加到进程 加载“附加进程”弹窗很慢
最近遇到一个问题,点击Ctrl + Alt + P 附加到进程的时候,弹出下图弹窗“附加到进程”很慢. 找了很多原因,后来发现,是因为少安装了一个插件,安装后,弹窗的耗时明显少了. 下载 Win ...
- matlab读取csv文件数据并绘图
circle.m(画二维圆的函数) %该函数是画二维圆圈,输入圆心坐标和半径%rectangle()函数参数‘linewidth’修饰曲线的宽度%'edgecolor','r',edgecolor表示 ...
- MySql数据库在NodeJS中简单的基本操作
阅读目录 一:连接数据库 二:数据的增删改查操作 2.1 数据库新增和查询数据 2.2 获取该数据的主键值 2.3 多语句查询 回到顶部 一:连接数据库 const mysql = require(' ...
- Vultr CentOS 7 安装 Docker
前言 最近在梳理公司的架构,想用 VPS 先做一些测试,然后就开始踩坑了!我用 Vultr 新买了个 VPS. 安装的 CentOS 版本: [root@dbn-seattle ~]# cat /et ...
- Java连接数据库,及增删改查
自定义连接数据库的util类 package com.shuzf.jdbc; import java.sql.Connection; import java.sql.DriverManager; im ...
- centos 6.8 搭建svn服务器
1. yum remove subversion #卸载 svn服务 2.下载svn服务器安装包 yum -y install subversion 3.创建SVN版本库 mkdir -p /opt/ ...
- react性能优化
前面的话 本文将详细介绍react性能优化 避免重复渲染 当一个组件的props或者state改变时,React通过比较新返回的元素和之前渲染的元素来决定是否有必要更新实际的DOM.当他们不相等时,R ...
- this指针详解
什么是this this是一个const指针,存的是当前对象的地址,指向当前对象,通过this指针可以访问类中的所有成员. 当前对象是指正在使用的对象,比如a.print(),a就是当前对象. 关于t ...
- 初识并发编程 MPI
MPI是一个跨语言的通讯协议,用于并发编程.MPI标准定义了一组具有可移植性的编程接口. 安装环境 MPICH 是开源的消息传递接口(MPI)标准的实现. 下载地址 # 解压文件 tar -xzvf ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...