题目:

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. SQLSERVER 数据类型int、bigint、smallint 和 tinyint范围

    [bigint] 从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字).存储大小为 8 个字节. [int ...

  2. python为什么叫胶水语言?python为什么是系统脚本?

    python为什么叫胶水语言?python为什么是系统脚本?   特点是什么? python现在最广为闻名的形容大概有这些: 他是很好的胶水语言.什么是胶水语言?反正当时的我不知道. 他是新一代的系统 ...

  3. [控件] Firemonkey 跨平台 Toast

    控件说明:一个简单的讯息提示功能,使用 FMX 基本控件,因此支持 Win, macOS, iOS, Android 平台. 已知问题:如果使用了 WebBrowser, MapView... 等原生 ...

  4. 调试大叔V2.1.0(2018.12.17)|http/s接口调试、数据分析程序员辅助开发神器

    2018.12.17 - 调试大叔 V2.1.0*升级http通讯协议版本,完美解决Set-Cookie引起的系列问题:*新增Content-Type编码格式参数,支持保存(解决模拟不同网站或手机请求 ...

  5. 易语言中锐浪报表绿色发布指南(免COM组件DLL注册)

    第一步 打开易语言开发环境(注要求易语言版本 v5.5及以上版本,如果版本太低,请更新到新版本),点击[工具]菜单,进入系统配置界面: 第二步 切换选择夹到[存根]项目,选中"使用用户自定义 ...

  6. HDFS上传数据的流程

    1.当客户端输入一条指令:hdfs dfs -put text.txt /text时,这条命令会给到DistributeFileSystem. 2.通过DistributeFileSystem简称DF ...

  7. ETL项目2:大数据清洗,处理:使用MapReduce进行离线数据分析并报表显示完整项目

    ETL项目2:大数据清洗,处理:使用MapReduce进行离线数据分析并报表显示完整项目 思路同我之前的博客的思路 https://www.cnblogs.com/symkmk123/p/101974 ...

  8. 【转】深入学习Redis(1):Redis内存模型

    原文:https://www.cnblogs.com/kismetv/p/8654978.html 前言 Redis是目前最火爆的内存数据库之一,通过在内存中读写数据,大大提高了读写速度,可以说Red ...

  9. 2017-2018-1 20155331 课下测试(ch10)

    2017-2018-1 20155331 课下测试(ch10) 假设下面代码中的foobar.txt中有6个ASCII字母,程序的输出是(A) Image 7.png A . c = f B . c ...

  10. Windows10放开Administrator权限

    手机上大家都喜欢使ROOT权限,root是超线用户的意思,但是Win10最高权限是Administrator管理员权限,但是系统默认是没有开启这个权限的需要系统安装好以后再次去开启. 方法/步骤 在桌 ...