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

Input: x = 1, y = 4

Output: 2

Explanation:

1   (0 0 0 1)

4   (0 1 0 0)

↑   ↑

思路:就是求两数异或的二进制表示中有几个1.

如何求一个整数的二进制表示有几个一呢?

while(x){x=x&(x-1);count++;}

一个数减一之后,会把它最低位的一个1变成0,再与上它自己就会消掉最低位的1。

4. leetcode 461. Hamming Distance的更多相关文章

  1. LeetCode:461. Hamming Distance

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

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

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

  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 汉明距离

    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. [css 实践篇] CSS box-orient

    定义和用法 box-orient 属性规定框的子元素应该被水平或垂直排列. 提示:水平框中的子元素从左向右进行显示,而垂直框的子元素从上向下进行显示.不过,box-direction 和 box-or ...

  2. CoolBlog开发笔记第3课:创建Django应用

    教程目录 1.1 CoolBlog开发笔记第1课:项目分析 1.2 CoolBlog开发笔记第2课:搭建开发环境 前言 经过上一节我们已经创建了CoolBlog工程,但是关于CoolBlog的功能代码 ...

  3. Java学习笔记--监视目录变化

    1.在实际开发中可能会需要监视某个目录下的文件所发生的变化.   2.在java7之前的做法 在一个独立的线程中使用File类的listFiles方法来定时检查目录中的内容,并与之前的内容进行比较   ...

  4. react实现双向绑定

    双向绑定功能是在项目中比较常见的一个需求,传统的js实现方式是添加监听函数的方式,Vue框架实现很简单,因为它本身就是基于双向绑定实现的,接下来我将讲解一下如何使用react实现双向绑定的功能是 首先 ...

  5. jquery fadeIn用法

    $("#msgSpan").fadeIn("slow"); setTimeout('$("#msgSpan").hide("slo ...

  6. js脚本中try与cache捕获异常处理

    <script type="text/javascript"> function add_reason(elm){ try{ var pp=$('.pp').val() ...

  7. 2.vue 安装教程

    安装node.js 从node.js官网下载并安装node,安装过程很简单,一路"下一步"就可以了(傻瓜式安装). 安装完成之后,打开命令行工具,输入 node -v,如下图,如果 ...

  8. 第一个spark+scala程序

    import org.apache.spark._import SparkContext._import java.util.{Calendar,Properties,Date,Locale}impo ...

  9. Host 'hello-PC' is not allowed to connect to this MySQL server远程连接mysql授权

    问题:"Host 'admin-PC' is not allowed to connect to this MySQLserver"   (其中,admin-PC为我的机器名) 原 ...

  10. c++ 命名空间 以及 作用域 函数参数 面向对象实验报告

    面向对象的分析与设计  实验报告一 一.变量的储存类别 auto static register extern auto变量   函数中的局部变量,如不专门声明为static存储类别,都是动态地分配存 ...