[Leetcode/Javascript] 461.Hamming Distance
[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的个数上。有两个方法:
- 将num和0x1进行与操作,结果是1,代表num的最右位是1,否则是0,然后将num右移一位,循环判断,结束条件为num===0
- 将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的更多相关文章
- 【leetcode】461. Hamming Distance
problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...
- 【LeetCode】461. Hamming Distance 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...
- LeetCode之461. Hamming Distance
------------------------------------------------------------------ AC代码: public class Solution { pub ...
- LeetCode:461. Hamming Distance
package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- LeetCode 461. Hamming Distance (汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [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 ...
- 461. Hamming Distance(leetcode)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- nodejs使用MYSQL连接池,断线重连
两种方式解决1.你可以配置mysql的连接池 var mysql = require('mysql'); var pool = mysql.createPool({ host: 'localhost' ...
- python 爬取猫眼榜单100(二)--多个页面以及多进程
#!/usr/bin/env python # -*- coding: utf- -*- # @Author: Dang Kai # @Date: -- :: # @Last Modified tim ...
- assert函数和捕获异常
assert函数: C语言和C++都有一个专为调试而准备的工具函数,就是 assert()函数. 这个函数是在C语言的 assert.h 库文件里定义的,所以包含到C++程序里我们用以下语句: #in ...
- mac home brew install go
mac利器home brew安装Go 首先你得需要安装home brew和ruby环境(因为home brew依赖ruby) 如果没有请自行到链接安装 准备好之后就开始安装go了 brew updat ...
- 手动创建maven项目+cmd+webapp+tomcat
1.创建文件夹 2.在刚刚创建的文件夹目录下:创建一个scr文件夹和pom.xml文件 3.在pom.xml配置 <?xml version="1.0" encoding=& ...
- 通过tomcat配置访问本机资源
背景:在最近的项目中使用海康威视的摄像机,在项目预算中没有购买对应的硬盘录像机,但是由于客户需要能够进行视频的回放功能,所以直接采用了海康提供的视频管理客户端直接进行视频录像,然后保存在本机进行播放. ...
- 理解volatile与synchronized
用 volatile 修饰的变量可以保证线程的"可见性",也就是,任何线程修改了这个 volatile 修饰的值都会通知其他线程来主缓存中重新读取值. 下面通过例子加以说明: pu ...
- TCP/IP协议之http和https协议
一.TCP/IP协议 TCP/IP 是不同的通信协议的大集合. 1.TCP - 传输控制协议 TCP 用于从应用程序到网络的数据传输控制. TCP 负责在数据传送之前将它们分割为 IP 包,然后在它们 ...
- 字符串的输入问题 C++
C++中,初学时最常用的输入字符的方式为cin,但是,cin是如何确定已经完成了字符串的输入了呢?由于不能通过键盘输入空字符("\0"),因此cin需要用别的方法来确定字符串的结尾 ...
- easyPOI导出excel报错
http-nio--exec- at :: - excel cell export error ,data is :com.jn.ssr.superrescue.web.qc.dto.Automati ...