LeetCode——Hamming Distance

Question

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 ≤ x, y < 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.

具体实现

class Solution {
public:
int hammingDistance(int x, int y) {
int tmp = x ^ y;
int res = 0;
while (tmp > 0) {
if (tmp % 2 != 0)
res++;
tmp = tmp >> 1;
}
return res;
}
};

LeetCode——Hamming Distance的更多相关文章

  1. [LeetCode] Hamming Distance 汉明距离

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

  2. LeetCode Hamming Distance

    原题链接在这里:https://leetcode.com/problems/hamming-distance/ 题目: The Hamming distance between two integer ...

  3. [LeetCode] Total Hamming Distance 全部汉明距离

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

  4. LeetCode Total Hamming Distance

    原题链接在这里:https://leetcode.com/problems/total-hamming-distance/ 题目: The Hamming distance between two i ...

  5. LeetCode 461. Hamming Distance (汉明距离)

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

  6. [Leetcode/Javascript] 461.Hamming Distance

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

  7. [LeetCode] 477. Total Hamming Distance 全部汉明距离

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

  8. [LeetCode] 461. Hamming Distance 汉明距离

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

  9. 【LeetCode】461. Hamming Distance 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 方法一:异或 + 字符串分割 方法二: ...

随机推荐

  1. 55、Android网络图片 加载缓存处理库的使用

         先来一个普通的加载图片的方法. import android.annotation.SuppressLint; import android.app.Activity; import and ...

  2. java关于Timer schedule执行定时任务 !!!!!!!!!

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等.对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Time ...

  3. selenium 方法导图

  4. 【Trello】使用指南

    一.谷歌浏览器插件推荐: Card Color Titles for Trello 在Card上现实标签的标题文字 Trellists: Trello Lists Master 允许展示和隐藏List ...

  5. Django设置中文,和时区、静态文件指向

    #========================================================== # 设置时区 注意注释上面的:LANGUAGE_CODE.TIME_ZONE.U ...

  6. python之MongoDB学习

    import pymongo as pm # 获取连接 client = pm.MongoClient('localhost', 27017) # 端口号是数值型 # 连接数据库 db = clien ...

  7. [NSUserDefaults]的使用:登陆后不再显示登录界面。

    简介: NSUserDefaults是IOS应用用来存储用户偏好和配置信息的途径,就像是一个数据库,但是它通过键值对(key-value)的方式存储. 比如["Thematrix" ...

  8. Scala 常用语法

    Clojure首先是FP, 但是由于基于JVM, 所以不得已需要做出一些妥协, 包含一些OO的编程方式 Scala首先是OO, Java语法过于冗余, 一种比较平庸的语言, Scala首先做的是简化, ...

  9. Qt 打印机支持模块

    Qt 打印支持 Qt为打印提供广泛的跨平台支持.使用每个平台上的打印系统,Qt应用程序可以打印到连接的打印机,并通过网络打印到远程打印机.Qt的打印系统还支持PDF文件生成,为基本的报告生成设施奠定了 ...

  10. js中的整除运算

      Math.ceil(count / pagesize); //向上整除 4/3=2;   Math.floor(count / pagesize); //向下整除 4/3=1; Math.roun ...