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 ...
随机推荐
- ideal中把项目打成war包,并放在tomcat运行,遇见的问题。。。
先说下我遇见的问题吧:最近做项目要把项目放在tomcat上运行,用的springboot框架, 在建项目时选择的是 jar包,项目写完要部署打包是,在pom中虽然把包改成了war ,可是每次放入to ...
- SpringMVC项目读取不到外部CSS文件的解决办法及总结
昨天,在写一个新需求的时候,因为没有前端同事的对接,无奈只有自己写css,js放到ssm结构的后台项目中,因为之前开发都是把前端文件放在一个专门的服务器上,一直没有关注服务端项目读取静态资源的问题,运 ...
- 使用Kernel NetEm和tc模拟复杂网络环境
关键词:netem(Network Emulator).tc(Traffic Control). 大部分局域网环境良好,但是产品实际网络环境可能千差万别,为了对产品进行各种情况测试就需要模拟网络环境. ...
- ExaWizards 2019 English D - Modulo Operations(DP)
Time Limit: 2 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement Snuke has a blackb ...
- vue通过自定义指令 v-py 将名字转拼音
自定义指令 py: 1.新建 vue-py.js文件 import Vue from 'vue'; var chinesePointCode = { "a": [21834, 38 ...
- 一、rollup
参考:reduxreach-routerrollup-starter-librollup-starter-approller-clicreate-react-library 一.安装 npm inst ...
- 剖析height百分比和min-height百分比
height的百分比 当我们给块元素设置百分比高度时,往往没能看到效果.因为百分比的大小是相对其最近的父级元素的高的大小,也就是说,其最近的父级元素应该有一个明确的高度值才能使其百分比高度生效. &l ...
- Linux(Ubuntu 16) 下Java开发环境的配置(二)------Tomcat的配置及常见问题
前言 相比于java JDK的配置,Tomcat的配置简单的多,简直就相当于直接运行了,本文以Tomcat8.0为例进行配置 1.Tomcat的下载 地址:https://tomcat.apach ...
- DAY10、函数的参数
一.实参:为形参传递值 调用函数时,实参可以由常量,变量,表达式三种的组合 1.位置实参:必须按照位置的顺序,从左到右为形参传递值 fn1(10, 20, 30) fn1(30, 20, 10) 2. ...
- 三、调试IIS启动域名配置
一.IIS配置启动VS以及域名 1.hosts配置 2.配置 注意: 1.Web和Api 端口在IIS都设置80即可,都可以同时运行不冲突,与vs的IIS express启动方式不同vs会指定不同的两 ...