[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 ...
随机推荐
- 【转】Xcode真机测试could not find developer disk image解决方法
在使用Xcode进行真机调试的时候,有时根据真机的系统不同,会出现could not find developer disk image 错误,这是由于真机系统过高或者过低,Xcode中没有匹配的配置 ...
- python 添加 threadpool
操作系统: Ubuntu 10.04 python安装依赖的软件包: python 出现 ImportError: No module named ** 我这里出现了: ImportError: No ...
- LeetCode(Two Sum)
一.题目要求 Given an array of integers, return indices of the two numbers such that they add up to a spec ...
- ipython notebook的使用
刚开始使用python,用的是ipython notebook,感觉很好用. 写的程序主要是处理文件的,读写txt文件,生成xml文件,其中参考http://www.cnblogs.com/wangs ...
- BZOJ1061: [Noi2008]志愿者招募(线性规划)
Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 5725 Solved: 3437[Submit][Status][Discuss] Descript ...
- 01U盘PE系统制作方法
1. 需要的工具和安装包:WinPE镜像文件 WinPE_x86.iso .已制作好的另一个启动盘(下文以映像总裁为例,当然也可以使用大白菜.U启动等) . 电脑.准备制作PE系统的空U盘 2. 还原 ...
- 7-1 python 操作redis
1.安装并导入redis模块 # pip install redis 安装redis模块 import redis # 导入redis模块 2.连接一个或多个redis,指定数据库名,并指定返回字符串 ...
- jquery横向手风琴效果2
<!doctype html> <html> <head> <meta charset="utf-8"> <script ty ...
- 开发工具cfree安装报错解决
报错如下: [ --------------------配置: mingw5 - CUI Debug, 编译器类型: MinGW-------------------- 检查文件依赖性... 正在编译 ...
- mount加载虚拟机增强工具步骤
1.创建一个挂载目录sudo mkdir /mnt/cdrom 2.在C:\Program Files\Oracle\VirtualBox\VBoxGuestAdditions.iso中添加该文件 3 ...