题目:

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. 【PHP函数】json_decode() ---- 对 JSON 格式的字符串进行解码

    json_decode() --- 对 JSON 格式的字符串进行解码 1.用法: mixed json_decode ( string $json [, bool $assoc = false [, ...

  2. Spring源码分析(十三)缓存中获取单例bean

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 介绍过FactoryBean的用法后,我们就可以了解bean加载的过程了 ...

  3. POJ3268(Dijkstra_邻接矩阵)

    https://vjudge.net/problem/POJ-3268 题目大意: n个农场的n头奶牛将前往x农场,要选择一条来回时间最短的路径. (一头牛的返回路线可能不同于她最初去派对的路线,因为 ...

  4. C++类型转换符重载

    对于用户自定义的类类型,实现它们和其他数据类型之间的转换有两种方法:(1)通过转换构造函数进行类型转换:(2)通过类型转换函数进行类型转换:转换构造函数:    类名(待转换类型) { 函数体; } ...

  5. android创建目录和文件和安装其它apk

    一.android下创建目录 File sd=Environment.getExternalStorageDirectory(); String path=sd.getPath()+"/no ...

  6. 用javascript制作2048游戏的思路(原创若 转载请附上本链接)

    一.项目已上传至github,地址:https://github.com/forjuan/2048game 二.学习了javascript基础后,想要捣鼓点东西做,做了一个自己以前很爱玩的2048游戏 ...

  7. iOS url出现特殊字符处理 -- stringByAddingPercentEncodingWithAllowedCharacters

    stringByAddingPercentEscapesUsingEncoding(只对 `#%^{}[]|\"<> 加空格共14个字符编码,不包括”&?”等符号), i ...

  8. Linux—echo命令

    echo命令的功能是在屏幕上显示一段文字,起到一个提示作用,常用在脚本语言和批处理文件中来在标准输出或者文件中显示一行文本或者字符串. 命令格式:echo [选项] 字符串 选项参数: -n:不在最后 ...

  9. Scala-构造函数

    /*scala的构造函数分为主构造函数和辅助构造函数. 一.主构造函数在Scala中,每个类都有主构造函数,和类的定义交织在一起.一个Scala类的主构造函数包括:1.构造函数的参数:2.类体中调用的 ...

  10. R语言学习笔记(二十二):字符串处理中的函数对比(代码实现)

    字符串处理中基本函数的使用 R自带函数与stringr包函数对比 > states <- row.names(USArrests) > # 提取字符串子集 > substr(x ...