题目:

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.

题目大意:

给你两个数字,返回这两个数字的汉明距离,汉明距离就是看两个数字的二进制代码,按位异或,比如1和4,二进制分别为(0001)和(0100),一个个异或比较后得到(0101)因为第二位和第四位不一样。然后数一下得到的数有几个1.这里有两个1,所以返回2

解法:

这是一个简单的题目,先定义一个计数器和一个记录两个数字异或的变量。即int cnt=0;int e=x^y;

然后数一下e的二进制代码里面有几个1,我的做法是让它与1按位与,如果和1按位与是1,那么就说明当前最后一位是1,如果不是,那当前最后一位为0.然后再左移,直到它变为0

比如说1和4,按位与后得到的e==5(二进制为0101)

1)与1按位与,得到1,则cnt+1;

2)右移一位,得到(010),与1按位与后得到0,则cnt不改变;

3)右移,得到(01),与1按位与得到1,cnt+1;

4)右移,得到(0),与1按位与得到0,cnt不变;

5)此时e==0,退出循环,返回cnt的值,即2.

代码:

int hammingDistance(int x, int y) {
    ,e=x^y;
    while(e){
        )
            ++r;
        e>>=;
    }
    return r;
}

从0开始的LeetCode生活—461-Hamming Distance(汉明距离)的更多相关文章

  1. [Leetcode/Javascript] 461.Hamming Distance

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

  2. 【leetcode】461. Hamming Distance

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

  3. [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 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...

  5. 461. Hamming Distance(汉明距离)

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

  6. LeetCode之461. Hamming Distance

    ------------------------------------------------------------------ AC代码: public class Solution { pub ...

  7. LeetCode:461. Hamming Distance

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

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

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

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

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

  10. 4. leetcode 461. Hamming Distance

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

随机推荐

  1. jQuery.extend 函数使用详解

    JQuery的extend扩展方法:      Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解.      一.Jquery的扩展方 ...

  2. php composer的学习之路(一)

    composer的介绍请看这里  http://docs.phpcomposer.com/00-intro.html composer的安装过程我就不介绍了,windows系统下跟其他安装没区别,一直 ...

  3. C#图解教程 第二十五章 其他主题

    其他主题 概述字符串使用 StringBuilder类把字符串解析为数据值关于可空类型的更多内容 为可空类型赋值使用空接合运算符使用可空用户自定义类型 Main 方法文档注释 插入文档注释使用其他XM ...

  4. Axure 入门学习

    Axure RP是美国Axure Software Solution公司旗舰产品,是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或Web网站的线框图.流程 ...

  5. 配置maven环境出现ARP tomcat native library 版本安装跟需求版本不一致时的解决方法An incompatible version xxxx of the APR based Apache Tomcat Native library is installed, while Tomcat requires version xxxx

    此地址下载你所需要的library版本http://archive.apache.org/dist/tomcat/tomcat-connectors/native/ 点击binaries 点win32 ...

  6. tomcat查看端口被占用

    1. tomcat有安装版和压缩版 安装版的没有关闭命令 压缩版的命令如下: tomcat关闭:catalina stop/shutdown 开启:catalina start   2. 在tomca ...

  7. 【BZOJ3930】选数(莫比乌斯反演,杜教筛)

    [BZOJ3930]选数(莫比乌斯反演,杜教筛) 题面 给定\(n,K,L,R\) 问从\(L-R\)中选出\(n\)个数,使得他们\(gcd=K\)的方案数 题解 这样想,既然\(gcd=K\),首 ...

  8. HashMap中的resize以及死链的情况

    之前我已经写过关于HashMap的内容了:http://www.cnblogs.com/wang-meng/p/7545725.html 我们都知道HashMap是线程不安全的, 如果多线程来访问会有 ...

  9. SpringMVC【参数绑定、数据回显、文件上传】

    前言 本文主要讲解的知识点如下: 参数绑定 数据回显 文件上传 参数绑定 我们在Controller使用方法参数接收值,就是把web端的值给接收到Controller中处理,这个过程就叫做参数绑定.. ...

  10. for语句 2017-03-17

    一.for语句 For(初始条件:循环条件:状态改变) { 循环体 } 步骤: 1.  先判断条件 2.  如果满足条件,执行循环体 3.  状态改变 例题: 1. i++和++i 的区别: var ...