[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. python学习接口测试(二)

    .python接口之http请求 python的强大之处在于提供了很多的标准库以及第三库,本文介绍urllib 和第三库的requests. Urllib 定义了很多函数和类,这些函数和类能够帮助我们 ...

  2. 如何不安装SQLite让程序可以正常使用

    System.Data.SQLite.dll和System.Data.SQLite.Linq.dll不必在GAC里面,关键在于Machine.config的DBProviderFactories没有正 ...

  3. Action 语法的简介

    https://www.cnblogs.com/LipeiNet/p/4694225.html https://www.cnblogs.com/Gyoung/archive/2013/04/04/29 ...

  4. c#右键窗体弹出菜单

    在工具箱(快捷键ctrl+w+x)——菜单和工具栏中找到 在属性中用这个绑定 然后写后台代码

  5. logistic regression (逻辑回归) 概述

    :http://hi.baidu.com/hehehehello/blog/item/0b59cd803bf15ece9023d96e.html#send http://en.wikipedia.or ...

  6. 配置SSIS 包部署

    包配置是干嘛滴! 使用包配置可以从开发环境的外部设置运行时属性和变量.         把用户变量转换成Config文件 步骤: 准备工作 把第一个例子中的userinfo.txt复制两份,放到同一个 ...

  7. linux下Mycat的安装配置

    1.下载Mycat Linux版:下载链接 2.通过SSH直连工具把安装包丢到linux:/usr/local/ 3.解压安装Mycat 4.配置环境 5.使配置文件生效

  8. java异常处理 throw RuntimeException时不需要同时方法中声明抛出throws 异常等待调用者catch进行捕获 子父类异常问题

    package com.swift.exception1; public class Demo_Exception { public static void main(String[] args) { ...

  9. 图的m着色

    图的m着色 #include <bits/stdc++.h> using namespace std; int n, k, m, ans; struct node{ int m, colo ...

  10. spring-mybatis整合项目 异常处理2

    org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'com/imooc ...