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. 大数据技术大合集:Hadoop家族、Cloudera系列、spark、storm【转】

    大数据我们都知道hadoop,可是还会各种各样的技术进入我们的视野:Spark,Storm,impala,让我们都反映不过来.为了能够更好 的架构大数据项目,这里整理一下,供技术人员,项目经理,架构师 ...

  2. Sublime text 3 快捷键:

    Ctrl+Shift+[  选中代码,按下快捷键,折叠代码. Ctrl+Shift+]  选中代码,按下快捷键,展开代码.  Ctrl+Shift+P:打开命令面板 Ctrl+P:打开搜索框,搜索项目 ...

  3. 判断 null undefined NaN

    1.判断undefined: var tmp = undefined; if (typeof(tmp) == "undefined"){ alert("undefined ...

  4. ES6入门概览一

    一.let const 1.let命令:声明变量仅在块级作用域有效:let实际上为js新增了块级作用域 好处: -不存在变量提升: -内部声明的变量 函数 不会影响外部 -不可重复声明变量 2.con ...

  5. 【IDEA】Maven踩坑:pom文件中的默认profiles不生效+IDEA中Maven的profiles使用说明

    一.问题即分析 项目pom文件中的profiles有3个配置:dev.test和production 默认配置的是dev,如下图: 但在本地起服务时,读取的配置始终是test里的. 二.原因 2.1 ...

  6. 接口测试工具 — jmeter(数据库操作)

    1.导入jdbc jar包 2.配置MySQL连接 3.执行sql语句

  7. JavaScript 入门之常见对象

    常见对象 1. Object 对象 2. String 对象 3. Array 对象 4. Date 对象 5. Number 对象 6. 自定义对象 with 语句 为了简化对象调用内容的书写 格式 ...

  8. 《深入理解Linux内核》阅读笔记 --- Chapter 2 Memory Addressing

    1.logical address = segment identifier (16bits) + offset (32bits) segment selector其实就是GDT或者LDT的索引,其中 ...

  9. MongoDB-6: MongoDB索引

    一.简介 在MongoDB建立索引能提高查询效率,只需要扫描索引只存储的这个集合的一小部分,并只把这小部分加载到内存中,效率大大的提高,如果没有建立索引,在查询时,MongoDB必须执行全表扫描,在数 ...

  10. DB2中编目本机其中数据库的方法

    问题:同一节点上有两个实例.假设想在当中一个实例下訪问还有一个实例中的数据库,有两种方法: 1. 使用catalog local node的方式,在当中一个实例中将另外一个实例直接编目,这样的方试中, ...