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 < 231.

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.
 public class Solution {
public int hammingDistance(int x, int y) {
int count = ;
for (int i = ; i <= ; i++) {
int value1 = (x >> i) & ;
int value2 = (y >> i) & ;
count += (value1 ^ value2);
}
return count;
}
}

Hamming Distance的更多相关文章

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

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

  2. [LeetCode] Hamming Distance 汉明距离

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

  3. Total Hamming Distance

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

  4. 461. Hamming Distance and 477. Total Hamming Distance in Python

    题目: The Hamming distance between two integers is the number of positions at which the corresponding ...

  5. LeetCode Total Hamming Distance

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

  6. LeetCode Hamming Distance

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

  7. LeetCode:461. Hamming Distance

    package BitManipulation; //Question 461. Hamming Distance /* The Hamming distance between two intege ...

  8. 64. 海明距离(Hamming Distance)

    [本文链接] http://www.cnblogs.com/hellogiser/p/hamming-distance.html [介绍] 在信息领域,两个长度相等的字符串的海明距离是在相同位置上不同 ...

  9. hduoj 4712 Hamming Distance 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4712 Hamming Distance Time Limit: 6000/3000 MS (Java/Other ...

随机推荐

  1. 使用Mysql Workbench 画E-R图

    MySQL Workbench 是一款专为MySQL设计的ER/数据库建模工具.你可以用MySQL Workbench设计和创建新的数据库图示,建立数据库文档,以及进行复杂的MySQL 迁移.这里介绍 ...

  2. C++中new,delete和new[] ,delete[]的分析

    转载在这里 http://www.cnblogs.com/hazir/p/new_and_delete.html

  3. favicon.ico 404的问题(title栏前面的图标)

    1.页面中自定义图标 去  http://www.bitbug.net/   定制图片,有32*32,16*16等样式可供选择 2.在页面中引入定义的图片 <link rel="sho ...

  4. UVA1225

    每增加1个整数,所有位上的对应数都加一.建立一个10000列10行的表,然后查表就可以了. #include<stdio.h> #include<string.h> ][]; ...

  5. 11 Set和Map数据结构

    Set和Map数据结构 Set WeakSet Map WeakMap 首先 这四个对象都是 数据结构. 用于存放数据 Set 类似数组. 但是不能重复. 如果你有重复数据,会自动忽略 属性 size ...

  6. github 相关英语

    github 相关英语 repository n. 仓库 A repository contains all the files for your project, including the rev ...

  7. 12月4日PHPCMS模板

    cms的样式有很多种,我们学习的是phpcms,这些cms都是大同小异,学会了一种就可以使用其它的cms. PHPCMS是一款网站管理软件.该软件采用模块化开发,支持多种分类方式,使用它可方便实现个性 ...

  8. ubuntu下gedit闪退,遇到问题:ERROR:../../gi/pygi-argument.c:1583:_pygi_argument_to_object: code should not be reached 已放弃 (核心已转储)

    解决方法:编辑->首选项关闭->插件->取消"多文件编辑"

  9. 耗电—Android

    Android应用耗电分析与优化 http://sanwen8.cn/p/297Ut7b.html http://www.cnblogs.com/kobe8/p/3819305.html 有效控制An ...

  10. 浅谈Android样式开发之布局优化

    引言 今天我们来谈一下Android中布局优化常用的一些手段.官方给出了3种优化方案,分别是</include>.</viewstub>.</merge>标签,下面 ...