LeetCode:461. Hamming Distance
package BitManipulation;
//Question 461. Hamming Distance
/*
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x and y, calculate the Hamming distance.
Note:
0 ≤ x, y < 231.
Example:
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
*/
public class hammingDistance461 {
public static int hammingDistance(int x, int y) {
int count=0;
while(x>0|y>0){
count+=(x&1)^(y&1);
x=x>>1;
y=y>>1;
}
return count;
}
//test
public static void main(String[] args){
int x=1;
int y=4;
System.out.println(hammingDistance(x,y));
} //study the solution of other people
//example1:use Integer.bitCount
public static int hammingDistance1(int x, int y) {
return Integer.bitCount(x ^ y);
}
//example2:xor = (xor) & (xor-1)
//4(100),1(001)->101&100=100->100&011=000
//1000001001这样中间的0可以一步直接跳过,机智!
public int hammingDistance2(int x, int y) {
int count = 0;
for(int xor = x^y; xor != 0; xor = (xor) & (xor-1)) count++;
return count;
}
}
LeetCode:461. Hamming Distance的更多相关文章
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- 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 bits ...
- 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 ...
- [LeetCode] 461. Hamming Distance(位操作)
传送门 Description The Hamming distance between two integers is the number of positions at which the co ...
- Leetcode - 461. Hamming Distance n&=(n-1) (C++)
1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...
- [Leetcode/Javascript] 461.Hamming Distance
[Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...
随机推荐
- Ubuntu 设置当前用户sudo免密码
方法1 # 备份 /etc/sudoers sudo cp /etc/sudoers . #打开 /etc/sudoers sudo visudo # 在文件末尾加入 kube ALL=NOPASSW ...
- percona-toolkit中在线ddl
percona-toolkit中在线ddl percona-toolkit工具提供了一组用于mysql操作的工具,比如主从复制,在线更改mysql表ddl等 一.安装1.安装perl(略)2.BI&a ...
- linux添加环境变量(centos)
1.查看当前环境变量 #echo $PATH 2.增加环境变量 #vi /etc/profile export PATH=/usr/path/bin:$PATH 3.生效 #source /etc/p ...
- RSA+DES动态加密
RSA可以用于加密,其加密强度很高,被人攻克的可能性极小.但是其加密速度很慢,如果对一段长数据进行加密是不现实的.因为无论加密还是解密都需要很长时间.所以通常是先用对称加密算法(DES, AES等)对 ...
- Java 获取汉字拼音的方法
package lius.util; import java.io.Serializable; import java.util.ArrayList; public class JString ...
- js 转码 和 .Net 后台解码
为防止 中文乱码,js传值要转码,当js 用 escape() 转码时,.Net 后台可以用 HttpUtility.UrlDecode() 进行解码. 例如: document.cookie = ...
- mongoperf用法
mongoperf是mongoDB自带工具,用于评估磁盘随机IO性能. 官方文档 使用方法 作用:用于部署前,评估mongodb所在存储的IO性能 用法:mongoperf <conffile, ...
- Python学习【第十一篇】模块(1)
模块 模块让你能够有逻辑地组织你的Python代码段. 把相关的代码分配到一个模块里能让你的代码更好用,更易懂. 模块也是Python对象,具有随机的名字属性用来绑定或引用. 简单地说,模块就是一个保 ...
- Windows 64位 RabbitMQ 安装配置
1:下载Erlang,地址:http://www.erlang.org/download/otp_win64_19.0.exe ,双击安装即可(首先装) 2:下载RabbitMQ,RabbitMQ 3 ...
- python之路 目录
目录 python python_基础总结1 python由来 字符编码 注释 pyc文件 python变量 导入模块 获取用户输入 流程控制if while python 基础2 编码转换 pych ...