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 ≤ xy < 231.

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. 用Java的位运算,
public class Solution {
public int hammingDistance(int x, int y) {
int sum=0;
while(x>0 || y>0)
{
int x1=x & 1;// 位与:第一个操作数的的第n位于第二个操作数的第n位如果都是1,那么结果的第n为也为1
int y1=y & 1; if((x1^y1)==1)// 第一个操作数的的第n位于第二个操作数的第n位 相反,那么结果的第n为也为1,否则为0
sum++;
y=y>>1; // 右移( >> ) 高位补符号位
x=x>>1;
}
return sum ;
}
}

461. Hamming Distance(汉明距离)的更多相关文章

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

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

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

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

  3. 【leetcode】461. Hamming Distance

    problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...

  4. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  5. [Leetcode/Javascript] 461.Hamming Distance

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

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

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

  7. [LeetCode] Hamming Distance 汉明距离

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

  8. 461. Hamming Distance and 477. Total Hamming Distance in Python

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

  9. 461. Hamming Distance(leetcode)

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

随机推荐

  1. visual svn 搭建

    详细出处参考:http://www.jb51.net/article/17365.htm 这里提示一个需要注意的地方: 在签入源代码到SVN服务器的时候: 点击Import,弹出下面的窗体(图2-2- ...

  2. msp430项目编程06

    msp430中项目---设计扫描键盘 1.扫描键盘工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(键盘驱动) 5.项目总结 msp430项目编程 msp430入门学习

  3. angularjs ngRoute的使用简单例子

    很丑的小例子,刚学angularjs,写下来方面以后看. 1.例子的工程目录如下: 2.index.html代码如下: <!DOCTYPE html><html><hea ...

  4. PHP中的魔术方法【转载】

    __construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep, __wakeup, _ ...

  5. php的错误控制运算符

    php的错误控制运算符 PHP中提供了一个错误控制运算符“@”. 可以将@放置在一个PHP表达式之前,该表达式可能产生的任何错误信息都被忽略掉: 如果开启了php.ini 中的 track_error ...

  6. Flex里监听mouseDownOutside事件解决弹出窗口点击空白关闭功能

    其实当用户在使用 PopUpManager 打开的某个组件外部单击时,会从该组件分派一个mouseDownOutside事件 监听该事件就能实现点击空白处关闭窗口的功能 this.addEventLi ...

  7. 【Java源码】树-概述

    树的基本术语 结点(node)由数据元素以及指向子树的地址构成. 若 X 结点有子树,则子树的根结点称为 X 的孩子(child)结点,相应地, X 称为其孩子的双亲(parents)结点,又称父母结 ...

  8. hdu 3237

    dp 状态压缩 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...

  9. java基础 3 Object通用方法(1)

    Object通用方法(1) clone: 浅复制    被复制对象的所有变量都含有与原对象相同的值,而所有对其他对象的引用仍然指向原来的对象,换言之,浅复制仅仅复                   ...

  10. FTRL (Follow-the-regularized-Leader)算法

    Online gradient descent(OGD) produces excellent prediction accuracy with a minimum of computing reso ...