题目:

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. Mac环境下安装配置Hadoop伪分布式

    伪分布式需要修改5个配置文件(hadoop2.x的配置文件$HADOOP_HOME/etc/hadoop) 第一个:hadoop-env.sh #vim hadoop-env.sh #第25行,由于新 ...

  2. Unity 4.7 导出工程在XCode9/10上报错 validateRenderPassDescriptor:644: failed assertion `XXXX'

    Unity 4.7 导出工程在XCode9/10上报错 validateRenderPassDescriptor:644: failed assertion `Texture at colorAtta ...

  3. 记一次ss无法上网的排查

    从日志开始排查. 登录服务器端 $ ssh root@[IP] 关闭 ss,再次启动并其指定日志输出文件 $ ssserver -c /etc/shadowsocks.json -d stop $ s ...

  4. Redis高级应用——2

    Redis-事务 Redis 事务可以一次执行多个命令, 并且带有以下两个重要的保证: 事务是一个单独的隔离操作,事务中的所有命令都会序列化.按顺序地执行.事务在执行的过程中,不会被其他客户端发送来的 ...

  5. 解决SDL/SDL.h: No such file or directory

    我在使用SDL2的时候遇到始终找不到头文件的问题,到处百度然后我尝试使用#include<SDL2/SDL.h>编译通过了,那么我很显然我之前设置的环境变量应该是没有生效的,后面在返回来研 ...

  6. IIC总线(集成电路总线)

    三大串行总线:UART.SPI.IIC(其中SPI是由时钟沿采集数据,为同步接口:UART和IIC是由电平采集数据,为异步接口) IIC速率:工作在半双工方式,2根线(SCL和SDA) 标准:100k ...

  7. 2017-2018-1 20155318 《信息安全系统设计基础》第九周课下实践——实现mypwd

    2017-2018-1 20155318 <信息安全系统设计基础>第九周课下实践--实现mypwd 相关知识 man -k 查找含有关键字的内容 与管道命令结合使用:man -k k1 | ...

  8. Java集合——TreeMap源码详解

    )TreeMap 是一个有序的key-value集合,它是通过红黑树实现的.因为红黑树是平衡的二叉搜索树,所以其put(包含update操作).get.remove的时间复杂度都为log(n). (2 ...

  9. [Oracle]如何查看一个数据文件是否是自动扩展

    开始 SQL> col file_id format 99SQL> col file_name format a50SQL> col tablespace_name format a ...

  10. 洛咕 P2467 [SDOI2010]地精部落

    同波浪,简单dp. 高度从1到n插入山脉,设f[i][j][k]表示插入了i个山脉,组成了j段,边界上有k个山脉的方案数. 那么新插入的山脉只会:插入在边界上且自己是一段.插入在边界上且与最左边的段相 ...