[Leetcode/Javascript] 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.


分析

题目很简单,给定两个32位的整数,求二进制位有几位不同。

思路是将两个数进行异或,然后计算异或出来的数(下文为num)二进制位有几个是1。

关键是在二进制位如何计算1的个数上。有两个方法:

  1. 将num和0x1进行与操作,结果是1,代表num的最右位是1,否则是0,然后将num右移一位,循环判断,结束条件为num===0
  2. 将num和num-1进行与操作,该操作会将num中最右边的为1的二进制位变为0(注意是最右边的1,而不是最右边的位),循环计算,结束条件为num===0

推荐第二种方法,第二种方法在leetcode上速度没有第一种快,但也打败了90%+的coder,虽然第二种稍微慢一点,但第一种算法会出现问题。

第一种方法的问题:我们都知道32位补码整数是负数的时候,最高位为1,两个负数还好,异或后结果为0,但如果只有一个负数,那么异或出来的num最高位为1,执行右移操作的时候会不断移进二进制位1,导致陷入死循环。

所以我个人推荐的是第二种方法,可以直接一个数字二进制位1的个数,而不用判断条件。


代码

/**
* @param {number} x
* @param {number} y
* @return {number}
*/
// 使用异或可以得到每个位上出现不同1的数
// 把一个整数减去1再和自身做与运算,会把该整数最右边的1变成0.
// 也可以使用和1做与操作,然后数字右移一位,但是如果输入是负数会无限循环(负数右移进来的位是1)
var hammingDistance = function(x, y) {
var count = 0;
var n = x ^ y;
while (n) {
++count;
n = (n - 1) & n;
}
return count;
}; // test
console.log(hammingDistance(1, 4));

[Leetcode/Javascript] 461.Hamming Distance的更多相关文章

  1. 【leetcode】461. Hamming Distance

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

  2. 【LeetCode】461. Hamming Distance 解题报告(java & python)

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

  3. LeetCode之461. Hamming Distance

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

  4. LeetCode:461. Hamming Distance

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

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

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

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

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

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

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

  8. 4. leetcode 461. Hamming Distance

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

  9. 461. Hamming Distance(leetcode)

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

随机推荐

  1. Java-笔记1

    /* 对第一个java程序进行总结 1. java程序编写-编译-运行的过程 编写:我们将编写的java代码保存在以".java"结尾的源文件中 编译:使用javac.exe命令编 ...

  2. 第18章 SysTick—系统定时器—零死角玩转STM32-F429系列

    第18章     SysTick—系统定时器 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/ ...

  3. 121. Best Time to Buy and Sell Stock——Leetcode

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  4. rectified units

    from: http://en.wikipedia.org/wiki/Rectifier_(neural_networks) In the context of artificial neural n ...

  5. using namespace std 是什么意思?

    摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using   namespace   std   意思:   using   和namespace都是C++的关键词. ...

  6. struts2入门第一天----------配置环境

    放假之后有空就开始走上了三大框架的学习.第一个选择的框架是struts2.首先第一步当然是环境的配置.去apache官网把struts2下载下来.然后在自己的开发工具下创建一个web项目.在lib文件 ...

  7. 分享spring、spring boot、spring cloud一些学习资源,从基础知识到项目实战

    1.spring注解驱动开发,学习spring boot和spring cloud必备知识 链接: https://pan.baidu.com/s/1xhULzLlpkERhoMi1G5Lgfg 密码 ...

  8. Linux系统崩溃,数据迁移

    就在1小时前,处理了件如标题所述的麻烦事儿.吃完午饭,想对此作个总结,一来自己梳理下过程以便后面遇见类似的事可以 快速处理,二来同行的小伙伴们可以探讨下.故事是这样的,公司所在园区物业晚上断电8小时, ...

  9. 《Redis设计与实现》- RDB持久化

    Redis RDB持久化功能可以将Redis内存中的数据库状态保存到磁盘里面,避免数据意外丢失. 1. 手动生成 RDB 文件 有两个Redis命令可以用于生成RDB文件: SAVE,该命令会阻塞Re ...

  10. python学习之变量类型

    变量: 变量是保存在内存中的值,根据变量类型开辟不同的内存空间且只允许符合该数据类型的数据才可以被存储在该内存空间中 变量赋值: 在Python中定义变量时,无需像其他语言一样需要声明数据类型.每个变 ...