题目:

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 ≤ xy < 2^31.

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.

分析:

将两个数的二进制进行比较,不同的计数加一,最后返回总数。

由于1&1 = 1,1&0 = 0,可以分别将x,y和1做与运算得到x,y的最后一位的值,再做异或运算,然后再将x,y右移一位。因为题里限定了x,y大小,所以只32次循环就够了。

程序:

class Solution {
public:
int hammingDistance(int x, int y) {
int nums = ;
for (int i = ; i < ; i++){
if ((x & ) ^ (y & ))
nums++;
x = x >> ;
y = y >> ;
}
return nums;
}
};

LeetCode 461. Hamming Distance (C++)的更多相关文章

  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. 4. leetcode 461. Hamming Distance

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

  6. LeetCode 461 Hamming Distance 解题报告

    题目要求 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. Python学习笔记系列——读写文件以及敏感词过滤器的实现

    一.读文件 #打开文件,传入文件名和标识符,r代表读 f= open('\\Users\ZC\Desktop\zc.txt','r') #调用read方法一次性读取文件的全部内容,存入内存,用str对 ...

  2. STM32F103 ucLinux开发BOOT

    STM32F103 ucLinux开发BOOT STM3210E-EVAL官方开发板主芯片STM32F103ZET6: 片内512K Flash,地址0x0800 0000 ~ 0x0807 FFFF ...

  3. Use UMDH to identify memory leak problem

    We sometimes got memory leak problem, and we need to find the leaked memory, Here is a usful tool fr ...

  4. Tomcat出现需要输入账号和密码问题

    这里是端口冲突问题: 可以做一下几个解决方案: 第一:更好Tomcat/conf/server.xml文件里面的制定8080端口号,更改为你能记住的端口数: 第二:找到冲突的端口的进程,杀死这个进程, ...

  5. Delphi按名字调用方法高级解决方案

    转帖于https://lfzhs.iteye.com/blog/980200 按名字调用方法似乎一直以来都是大家比较关注的技术,在论坛上有一个经典的答复: type TProcedure = proc ...

  6. STM32的AFIO时钟什么时候需要开启

    相比于普通单片机,STM32 拥有复杂的时钟系统,相应的控制器称为 RCC(Reset Clock Controller,复位与时钟控制器).每个外设都配备了外设时钟的开关,当我们不使用某个外设时,可 ...

  7. SAP查找用户的登录记录

    1.可以使用USR02中有个上次登陆日期和登陆时间. 2.用SE38跑下RSUSR200,输入用户名即可查询上次登陆日期 3.SU10可以查到

  8. 马尔可夫毯(Markov blanket)

    马尔可夫毯(Markov blanket) 马尔科夫毯,是满足如下特性的一个最小特征子集:一个特征在其马尔科夫毯条件下,与特征域中所有其他特征条件独立.设特征T的马尔科夫毯为MB(T),则上述可表示为 ...

  9. mfc 函数重载

    函数重载的概念 for循环中变量 一. 函数重载的概念 函数重载允许我们使用相同的函数名定义多个函数. 提示: 函数参数类型不同,可重载. 类型相同时,则需要参数个数不同. int max(int a ...

  10. 9.22 下午 (document对象)

    document对象 1.找元素:(1)gerElementById()根据ID找   (2)gerElementByClassName()根据Class找,返回数组       (3)gerElem ...