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. iOS圆饼图和圆环的绘制,并且添加引线

    在开发中经常遇到统计之类的需求,特此封装了一个简单的圆饼图和圆环图,效果图如下 代码下载地址:https://github.com/minyahui/MYHCricleView.git

  2. Linux一些零碎

    1.设置时间和市区 1.tzselect 2.sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  3. Sping

  4. Git实用命令手册

    下载代码 git clone <git地址> 用户配置 git config —-global user.name <name> git config —-global use ...

  5. 弱网测试Android

    弱网测试一般是指模拟在网络环境比较差的情况下,检测APP是否有异常,如崩溃,数据收发出现丢包的情况 一.首先需要控制网络,有两种方式其一使用网络损伤仪进行,其二采用软件方式.硬件采购费用太贵,因此使用 ...

  6. 机器学习实战(一)kNN

    $k$-近邻算法(kNN)的工作原理:存在一个训练样本集,样本集中的每个数据都存在标签,即我们知道样本集中每一数据与所属分类的对于关系.输入没有标签的新数据后,将新数据的每一个特征与样本集中数据对应的 ...

  7. LeetCode Coin Change

    原题链接在这里:https://leetcode.com/problems/coin-change/ 题目: You are given coins of different denomination ...

  8. sys.stdout.write与sys.sterr.write(三)

    目标: 1.使用sys.stdout.write模拟"|"的顺时针变化- \ | / 2.使用sys.stderr.write模拟"|"的顺时针变化- \ | ...

  9. win7下开启telnet命令

    win7下开启telnet命令 win7上telnet这条命令默认被关闭了. 开启telnet方法如下: 一,打开控制面版 二,选择程序 三,选择打开或关闭windows功能 在弹出窗口中把 Teln ...

  10. CentOS 7 安装 nginx

    一.安装nginx依赖lib库: 1.yum install gcc-c++ -y 2.yum install pcre pcre-devel -y 3.yum install zlib zlib-d ...