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的更多相关文章

  1. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

  2. LeetCode 461. Hamming Distance (汉明距离)

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

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

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

  4. 4. leetcode 461. Hamming Distance

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

  5. LeetCode 461 Hamming Distance 解题报告

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

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

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

  7. [LeetCode] 461. Hamming Distance(位操作)

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

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

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

  9. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

随机推荐

  1. Rails--抛出异常

    begin ... rescue Exception => e ... end

  2. BAT实现服务器文件同步

    服务器文件同步有很多工具,例如 GoodSync.rsync.BitTorrent Sync等……其实WINDOWS下自带了一个文件同步利器:ROBOCOPY.它是一个命令行的目录复制命令,自从Win ...

  3. HTML中添加背景音乐

    <audio controls="controls" height="100" width="100"> <source ...

  4. QList

    #include <QCoreApplication> #include<QList> #include<QDebug> int main(int argc, ch ...

  5. WireShar使用笔记

    1.下载wiresharp  官网下载 2.安装 安装后直接支持中文 很人性化哦 注意:一定要安装WinPcap 不然无法使用 3.

  6. magento搬家

    将原来网站文件中的var文件中的cache和session文件删除,将media中的缓存文件删除.然后将所有文件制作成一个压缩包,以减少文件体积,方便转移. 将压缩包转移到新的服务器域名指向的文件夹, ...

  7. 如何整合最新的Flex sdk和Air sdk。

    使用Flex来开发air应用,如果想使用最新sdk的话,配置起来还真是一个麻烦事儿. Flex捐给apache维护了,air还是adobe自己维护,那么就得分别到这两边下载最新的sdk然后自己整合起来 ...

  8. CS193P - 2016年秋 第三讲 Swift 语言及 Foundation 框架

    这一讲介绍一些 Swift 的重点概念.特别是一些有别于其它语言的地方.但本质上还都是语法糖. 想充分理解这一讲的内容,最好的方式就是 打开 playgound,亲自动手来实验. 1,Optional ...

  9. shell常用命令归类整理

    shell 命令整理     bash shell 含有许多功能,因此有许多可用的命令:本文档仅罗列了一些常用命令及其使用频率较高的参数.#本文档仅罗列了一些常用命令及其使用频率较高的参数.#vers ...

  10. C++学习笔记 知识集锦(一)

    1.内存管理的开销 2.函数调用框架 3.类为什么要定义在头文件 4.C++的组合 5.在类的外部定义成员函数 6.bool类型为什么可以当做int类型 7.无符号保留原则 8.C++类型检查 9.何 ...