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

Input: x = 1, y = 4

Output: 2

Explanation:

1   (0 0 0 1)

4   (0 1 0 0)

↑   ↑

思路:就是求两数异或的二进制表示中有几个1.

如何求一个整数的二进制表示有几个一呢?

while(x){x=x&(x-1);count++;}

一个数减一之后,会把它最低位的一个1变成0,再与上它自己就会消掉最低位的1。

4. leetcode 461. Hamming Distance的更多相关文章

  1. LeetCode:461. Hamming Distance

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

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

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

  3. 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 ...

  5. LeetCode 461 Hamming Distance 解题报告

    题目要求 The Hamming distance between two integers is the number of positions at which the corresponding ...

  6. LeetCode 461. Hamming Distance (C++)

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  7. [LeetCode] 461. Hamming Distance(位操作)

    传送门 Description The Hamming distance between two integers is the number of positions at which the co ...

  8. Leetcode - 461. Hamming Distance n&=(n-1) (C++)

    1. 题目链接:https://leetcode.com/problems/hamming-distance/description/ 2.思路 常规做法做完看到评论区一个非常有意思的做法.用了n&a ...

  9. [Leetcode/Javascript] 461.Hamming Distance

    [Leetcode/Javascript] 461.Hamming Distance 题目 The Hamming distance between two integers is the numbe ...

随机推荐

  1. 深入理解YYCache

    前言 本篇文章将带来YYCache的解读,YYCache支持内存和本地两种方式的数据存储.我们先抛出两个问题: YYCache是如何把数据写入内存之中的?又是如何实现的高效读取? YYCache采用了 ...

  2. PHP加密解密数字

    <?php /** * 加密解密类,PHP加密解密数字,适用于URL加密 * 该算法仅支持加密数字.比较适用于数据库中id字段的加密解密,以及根据数字显示url的加密. * @version a ...

  3. 更改zendstudio花括号匹配显示的方法

  4. Python如何调用新浪api接口的问题

    前言:这些天在研究如何调用新浪开放平台的api分析新浪微博用户的数据 成果:成功调用了新浪api获取了用户的一些个人信息和无数条公共微博 不足:新浪开放平台访问有限制,返回的数据着实有限,不足以分析问 ...

  5. Oracle数据库中的重要对象

    数据库中的重要对象:表.视图.序列.函数.存储过程.索引.同义词1.表:用PL/SQL Developer 软件打开 Scott的DEPT表查看SQL,可以看见DEPT表创建的脚本-- Create ...

  6. Struts2请求参数合法性校验机制

    在Action中通过代码执行数据校验 请求参数的输入校验途径一般分两种:客户端校验 :通过JavaScript 完成 (jquery validation插件),目的:过滤正常用户的误操作. 服务器校 ...

  7. js 监控iframe URL的变化

    iframe的url可以前端任何地址,这样就可能出现漏洞,如果钓鱼网站通过js把src改成了危险地址,如果没有监控,就会有很大隐患.所以监控iframe的url变化就是必须要解决的问题了. 第一印象的 ...

  8. 00002、div的文字垂直居中与背景的渐变

    1.div可以放多行的文字,当显示文字较少时,文字可垂直居中 text-align: center; display: table-cell; vertical-align: middle; text ...

  9. [Hadoop源码系列] FairScheduler分配申请和分配container的过程

    1.如何申请资源 1.1 如何启动AM并申请资源 1.1.1 如何启动AM val yarnClient = YarnClient.createYarnClient setupCredentials( ...

  10. 基于.NET CORE微服务框架 -surging的介绍和简单示例 (开源)

    一.前言 至今为止编程开发已经11个年头,从 VB6.0,ASP时代到ASP.NET再到MVC, 从中见证了.NET技术发展,从无畏无知的懵懂少年,到现在的中年大叔,从中的酸甜苦辣也只有本人自知.随着 ...