Level:

  Easy

题目描述:

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

思路分析:

  汉明距离指的是两个数的二进制形式中对应位数值不同的数目。因此我们将两个数相异或,那么异或结果的二进制形式中非零位的个数就是汉明距离。

代码:

class Solution {
public int hammingDistance(int x, int y) {
int xor=(x^y);
int t=1;
int res=0;
while(t>0){
if((xor&t)!=0)
res++;
t<<=1;
}
return res;
}
}

12.Hamming Distance(汉明距离)的更多相关文章

  1. [LeetCode] Hamming Distance 汉明距离

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

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

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

  3. 461. Hamming Distance(汉明距离)

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

  4. 477 Total Hamming Distance 汉明距离总和

    两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...

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

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

  6. [Swift]LeetCode461. 汉明距离 | Hamming Distance

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

  7. [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

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

  8. Leetcode#461. Hamming Distance(汉明距离)

    题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...

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

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

随机推荐

  1. DAY10-MYSQL表操作

    一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 http://www.cnblogs.com/guoyunlong666/p/8491702.html 二 表介绍 表 ...

  2. hive删除表报错:Specified key was too long; max key length&nb

    我是在hive删除表的时候出现这个错误的,看到这个错误应该就知道是字符集错误. 但是我用​ alter database hive character set latin1; 这个命令将其改成拉丁之后 ...

  3. 第4章 springboot热部署 4-1 SpringBoot 使用devtools进行热部署

    /imooc-springboot-starter/src/main/resources/application.properties #关闭缓存, 即时刷新 #spring.freemarker.c ...

  4. Java线程池拒绝策略

    Java线程池拒绝策略 相关资料: 线程池的RejectedExecutionHandler(拒绝策略):http://blog.csdn.net/jgteng/article/details/544 ...

  5. Android之对话框Dialog

    首先是确认对话框 //确认对话框 private void showLog1() { AlertDialog.Builder dialog = new AlertDialog.Builder(this ...

  6. 一道java笔试题目:Vector和ArrayList的区别

    Vector和ArrayList的区别 线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构这些类均在java.util包中本文试图通过 ...

  7. Luogu 4216 [SCOI2015]情报传递

    BZOJ 4448. 写起来很愉悦的题. 按照时间可持久化线段树,修改就在原来的地方加$1$即可,查询就直接询问$root_1 - root_{now - c - 1}$中相应的个数. 主席树维护树链 ...

  8. 37LCD-TFTLCD原理与配置

    1.TFTLCD驱动原理 简介

  9. ubuntu 12.04 (64位)下安装oracle 11g过程及问题总结

    最近公司用到oracle,在ubuntu64位安装了一下,碰到了一些问题,在网上搜索到了一些答案,在此作为笔记记录下来. 1.首先下载oracle并解压不再赘述. 2.安装依赖包 sudo apt-g ...

  10. MSCN(Mean Subtracted Contrast Normalized)系数的直方图

    MSCN系数是无参考的空间域图像质量评估算法BRISQUE(No-Reference Image Quality Assessment in the Spatial Domain)中提出的,MSCN系 ...