[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 ...
随机推荐
- 2017.9.29 web网上答题及其自动评测系统
1. 设计计一个网上答题及其自动评测系统,首先是试题页面的设计及其解答的提交, 其次是当提交解答之后,系统自动评阅并给出结果. 分析:需要两个jsp页面:一个是提交信息的页面,另一个是获取提交信息的页 ...
- caffe RandomHue和RandomSaturation
https://www.cnblogs.com/wangyblzu/p/5710715.html HSV和RGB一样是一种图像的颜色模型,h表示色调,s表示饱和度 1.RandomHue void R ...
- 用蒙特卡洛方法计算派-python和R语言
用蒙特卡洛方法算pi-基于python和R语言 最近follow了MOOC上一门python课,开始学Python.同时,买来了概率论与数理统计,准备自学一下统计.(因为被鄙视过不是统计专业却想搞数据 ...
- sql学习之创建表空间创建用户并授权
--创建表空间语法:create tablespace [name]create tablespace hclTest--设置参数datafile 'F:/orcale/hclTest'--设置表空间 ...
- Vuex基础-Getter
官方地址:https://vuex.vuejs.org/zh/guide/getters.html Vuex 允许我们在 store 中定义“getter”(可以认为是 store 的计算属性).就像 ...
- matlab 读取文件(mat)存储为json文件
fid= fopen('reqJosn.json', 'w+'); load('request-set-10.mat'); requests = requests.request; requestNu ...
- cnn为什么会不存在vanishing gradient的问题
之前神经网络火过一段时间,但是后来又淡出了,后来又火了,尤其是到2012年真的像发水一样. 之前为什么不火了呢,因为人们发现网络浅了吧,没什么优势.网络深了吧,又会出现vanishing gradie ...
- css3 子元素的的应用 注意点
已经第二次犯错误,不允许有下次 <ul class="ul"> <li> <a>哈哈</a> </li> <li& ...
- 在centos7云服务器上搭建Apache服务器并访问到你的网站
使用X-shell ssh安全连接到云服务器 https://mail.qq.com/cgi-bin/mail_spam?action=check_link&url=https://www.n ...
- .NET向WebService传值为decimal、double、int、DateTime等非string类型属性时,服务器端接收不到数据的问题
最近在做CRM项目时,使用C#调用SAP PI发布的WebService服务时遇到的问题: 向WebService传值为decimal.double.int.DateTime等非string类型数据时 ...