首先附上题目链接: 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. veeValidate实战

    说在前面 vee-validate 版本2.0.4的学习github地址我的项目地址第一次认真的在git上写一个demo教程,喜欢的可以star一下~^o^~ (^-^) (^o^) 后续会有一个完整 ...

  2. 批量升级 CentOS bash

    #! /usr/bin/env python #coding=utf-8   from fabric.api import * from fabric.state import *   env.rol ...

  3. Codeforces Round #427 (Div. 2) [ C. Star sky ] [ D. Palindromic characteristics ] [ E. The penguin's game ]

    本来准备好好打一场的,然而无奈腹痛只能带星号参加 (我才不是怕被打爆呢!) PROBLEM C - Star sky 题 OvO http://codeforces.com/contest/835/p ...

  4. SparkSQL之UDAF使用

    1.创建一个类继承UserDefinedAggregateFunction类. ------------------------------------------------------------ ...

  5. 【Wince-ListView】Wince中的 ListView怎么显示网格?

    using System.Runtime.InteropServices; using System.Windows.Forms; namespace CETEST { public class Co ...

  6. C# Lambda表达式学习笔记

    本笔记摘抄自:https://www.cnblogs.com/leslies2/archive/2012/03/22/2389318.html,记录一下学习过程以备后续查用.     一.Lambda ...

  7. redis异步处理

    $reids = new Redis; $redis->connect('localhost',6379); $redis->auth(''); //将数组转换成字符串再存到redis中 ...

  8. 2019icpc南京网络赛 F 主席树

    题意 给一个\(n\)的全排列数组\(a\),求一个递推数组每一项的值:\(ans[i]=ans[j]+1\),\(j\)为\(a[pos[i]-k]到a[pos[i]+k],(pos[i]为i在数组 ...

  9. Codeforces 437D The Child and Zoo(并查集)

    Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...

  10. ajax-php跨域请求

    php: function __construct(){ // 指定允许其他域名访问 header("Access-Control-Allow-Origin: *"); heade ...