汉明距离
两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。
给出两个整数 x 和 y,计算它们之间的汉明距离。
注意:
0 ≤ x, y < 231.
示例:
输入: x = 1, y = 4
 
输出: 2
 
解释:
1 (0 0 0 1)
4 (0 1 0 0)
        ↑   ↑
 
上面的箭头指出了对应二进制位不同的位置。
 
 
^
如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。

思路:利用了异或的性质^   两个数字的二进制字符,比如   01001^10010=11011

异或^得到的结果中含有多少个1,就表示他的汉明距离。和上一道题:位1的个数 类似https://www.cnblogs.com/patatoforsyj/p/9475383.html

代码如下:

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

leetcode-汉明距离的更多相关文章

  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. LeetCode 461 汉明距离/LintCode 365 统计二进制中1的个数

    LeetCode 461. 汉明距离 or LintCode 365. 二进制中有多少个1 题目一:LeetCode 461. 汉明距离 LeetCode 461.明距离(Hamming Distan ...

  4. 【LeetCode】汉明距离(Hamming Distance)

    这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x,  ...

  5. [LeetCode] 477. Total 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 bits ...

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

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

  8. leetCode:461 汉明距离

    汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 思路: 当看到"对应二进制位不同的位置的数目"这 ...

  9. 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)

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

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

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

随机推荐

  1. STM32F103 ucLinux开发之四(内核启动后的调试)

    Stm32-uclinux启动后的调试 1.  修改__pfn_to_page使得能够启动 根据STM32F103 ucLinux开发之三(内核启动后不正常)的描述,内核无法启动是选择了平板内存模式后 ...

  2. type和 #define

    1.#define INT8U unsigned char   :  用INT8U代替unsigned char 2.typedef typedef int size; 此声明定义了一个int的同义字 ...

  3. mysql错误errno:121

    121错误是因为外键名重复.在同一个库中外键是不允许与其他外键重名的. 遇到这个错误请给你定义的外键换唯一无重复的名字. 同时查阅到外键也有可能导致150错误. Can't create table ...

  4. Java编码问题原因以及解决

    一.文件编码 Unicode 是首选编码.Unicode 是全球范围的字符编码标准. 小结: GBK 与unicode之间的转换是通过gbk unicode映射表. UTF-8 与unicode之间的 ...

  5. winform 里 如何实现文件上传

    看了网上写的 用webclient类的uploadfile方法, 我在本地建立了个webform,winform窗体,  现在可以本地实现文件传递,可以选择文件传到d:\temp路径下,但怎们传到服务 ...

  6. 1.Redis介绍以及安装

    Redis介绍 Redis是一个开源的(BSD开源协议),内存数据结构存储,被用于作为数据库,缓存和消息代理. Redis支持如下数据结构: string(字符串) hashes(哈希) lists ...

  7. 正则表达式-Regular expression学习笔记

    正则表达式 正则表达式(Regular expression)是一种符号表示法,被用来识别文本模式. 最近在学习正则表达式,今天整理一下其中的一些知识点 grep - 打印匹配行 grep 是个很强大 ...

  8. 同步请求和异步请求的区别(理解ajax用)

    同步请求:发送方发送数据包后,等待接收方发回响应之后,才能发送下一个数据包的通信方式. 异步请求:发送方发送数据包后,不用等待接收方发回响应,就可以发送下一个数据包的通信方式. 同步通信:要求通信双方 ...

  9. 集合之Map

    Map:存放键值对,根据键对象找对应的值对象.键不能重复!Map键不能重复,有唯一性,一般通过键找对应的的值Map集合的特点: 1.具有映射关系 2.两列 3.一列要唯一 一列可以重复 键类似于 Se ...

  10. 472. Concatenated Words

    class Solution { public: vector<string> res; vector<string> findAllConcatenatedWordsInAD ...