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 ...
随机推荐
- 一起来做webgame,《卡片魔兽》(一)基础战斗
写在前面的话 这不是教程,只是博主在娱乐过程中的一些小结记录.博主水平有限,没有什么高级的东西,只是将一些小的知识点结合一下,做这么一个养成类型的卡片页面游戏(=.=!有点绕).做一个完整的游戏,涉及 ...
- TID大会学习心得之敏捷软件架构-微服务
敏捷微服务构建 王威: TW咨询师.架构转型教练.敏捷技术教练 敏捷的目标 敏捷的目标是提升效率?降低成本?减员增效? 敏捷:关注价值.快速反馈.快速响应.其的目标是提升响应力,响应力的提升不一定会提 ...
- iOS上线...踩坑
总结一下上线过程中出现的问题: 1.AppStore不允许app中出现下载别的app的提示 (例如:三方登录的时候,检测到手机未安装QQ,微信,微博,提示你的设备未安装!❌❌❌ 正确的做法:未安装的, ...
- LeetCode Sum of Two Integers
原题链接在这里:https://leetcode.com/problems/sum-of-two-integers/ 题目: Calculate the sum of two integers a a ...
- spring-amqp 动态创建queue、exchange、binding
pom.xml <!-- mq 依赖 --> <dependency> <groupId>com.rabbitmq</groupId> <arti ...
- magento目录结构说明,Magento文件夹结构说明,Magento folder structure
/app – 程序根目录 /app/etc – 全局配置文件目录 /app/code – 所有模块安装其模型和控制器的目录 /app/code/core – 核心代码或经过认证 ...
- sql之连表查询--效率 通过分析各种连接查询的实现原理来了解
1. 左连接 2.右连接 3.内连接 4.Cross join 笛卡尔乘积
- Jquery父级节点追加
<!-- 父节点追加 --><!DOCTYPE html><html lang="en"><script src="../../ ...
- Webform Session Cookies状态保持
Request对象的五个集合: ①.QueryString:用以获取客户端附在url地址后的查询字符串中的信息. 例如:stra=Request.QueryString ["strUserl ...
- 清除SQL2008R2日志文件
最近公司的SQL数据库全转移为阿里云数据库,由于自己转移的时候是执行的脚本,所以产生了很多的日志文件,都是没用的日志文件,所以自己想清除日志,自己电脑没有安装SQL2008,所以远程公司其他安装SQL ...