首先附上题目链接: https://leetcode-cn.com/problems/hamming-distance/

一:题目

两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。给出两个整数 x 和 y,计算它们之间的汉明距离。

注意:0 ≤ x, y < 231.

示例:

输入: x = 1, y = 4

输出: 2

解释:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

上面的箭头指出了对应二进制位不同的位置。

  简单说明:这里就是要我们求两个数对应的二进制,有多少个位置的1是单独的,这里的单独意思是指两个数在某个位置是(0,1)形式存在,那这个1就是单独的。

         需要注意的是它给的x,y的范围,所以我们的变量只需要int就行了。

二:方法(c++)

  1:方法一:直接移位,每次取两个的最后一位进行判断

    这种二进制的题目,首先想到的就是能不能移位和亦或,或,与等操作。这里将每个数的最后一位比较,若是一个为0,一个为1则计数。

   int count=;
while(x!=||y!=)
{
if((x&)!=(y&))
{
count++;
}
x>>=;
y>>=;
}
return count ;

  2:方法二:汉明重量加汉明距离

    这里首先需要介绍这两个概念。汉明重量:某个数二进制中1的数量。  汉明距离:二进制位不同的位置的数目(就如题目所说,对应1的位置不同的数目)。

    汉明重量:n=n&(n-1)

      这里以6为例子:0110

      第一次;0110&0101=0100   :将最后一个1去掉了

      第二次:0100&0011=000    :将最后一位去掉了

    汉明距离:本质为两个数异或后字符"1"的个数,可以直接使用异或实现。

    所以这里我们首先异或算出哪些地方不同位置1。然后使用汉明重量,一次次统计异或得出的二进制里面1的个数。

int hammingDistance(int x, int y) {
//汉明距离加重量
int sum=x^y;// 异或来判断哪些不相同的,待会就是把不相同的一个个判别出来
int count=;
while(sum!=)
{
sum=sum&sum-;//依次去掉一
count++;
}
return count;
}

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

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

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

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

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

  3. 【leetcode 461】. Hamming Distance

    要求: 给定两个整数x和y,0 ≤ x, y < 231. 求x和y的汉明距离. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 ...

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

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

  5. [LeetCode] 461. 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] Total Hamming Distance 全部汉明距离

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

  8. [LeetCode] Hamming Distance 汉明距离

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

  9. 【leetcode】461. Hamming Distance

    problem 461. Hamming Distance solution1: 根据题意,所求汉明距离指的是两个数字的二进制对应位不同的个数.对应位异或操作为1的累积和. class Solutio ...

  10. [Leetcode/Javascript] 461.Hamming Distance

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

随机推荐

  1. JQUERY动态绘制表格,实现动态添加一行,删除一行

    HTML部分 <table style="width: 100%;" id="TABYESTERDAY11"></table> < ...

  2. vue基本知识点概括

    目录 Vue 渐进式 JavaScript 框架 一.走进Vue 1.what -- 什么是Vue 2.why -- 为什么要学习Vue 3.special -- 特点 4.how -- 如何使用Vu ...

  3. Codeforces 871C 872E Points, Lines and Ready-made Titles

    题 OvO http://codeforces.com/contest/871/problem/C ( Codeforces Round #440 (Div. 1, based on Technocu ...

  4. vue中单选框与多选框的实现与美化

    我们在做一些页面时,可能会用到很多的单选框和复选框,但是原生的radio和checkbox前面的原型图标或方框样式不尽人意.于是,决定自己来实现单选框和复选框.我用的是vue,所以就用vue的方式实现 ...

  5. Binary Protocol

    A. Binary Protocol 这道题要唯一注意的一点就是数字0的表示--0个"1"来表达,所以字符串"100"所表示的数字就是100 附代码: // C ...

  6. Bzoj3073Journeys

    这不裸的dij吗?来,弄他. 打完以后发现不妙,这数据范围略神奇……算一算,考一场都可能跑不出来.map去重边(成功额外引入log)不怕,交.TLE,54. 这不玩呢吗,把map去了,交.MLE,71 ...

  7. Git常用命令详解

    1.创建版本库 git clone <url> #克隆远程版本库 git init #初始化本地版本库 通过 ls -ah 可以看到隐藏的.git目录 2.修改和提交 添加文件readme ...

  8. beautifulsoup 安装

    pip install beautifulsoup4

  9. beta week 2/2 Scrum立会报告+燃尽图 05

    此作业要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9957 一.小组情况 组长:贺敬文组员:彭思雨 王志文 位军营 徐丽君队名: ...

  10. FastAdmin 在 CRUD 时出现 exec() has been disabled for security reasons 处理方法

    然后在看看 禁用函数列表(php.ini)disable_functions = proc_open, popen, exec, system, shell_exec, passthru 这里要把 e ...