看knn算法时无意间发现这个算法,但是维基上有错误的示例和python代码。。。因为汉明距离并不是求相同长度字符串(或相同长度的整数)之间的字符(或数位)差异个数。

  正确的详见:https://en.wikipedia.org/wiki/Talk:Hamming_distance

  然而,我发现百度百科和一些博客都是参考的汉明距离-维基百科,所以都有错 = =。。。

  认真分析正确代码后,我认为汉明距离指的是将两个字符串或两个整数编码为一组二进制数,然后计算两二进制bit之间的差异个数。

  给一个维基上的正确的代码吧,省的大家麻烦:

s1 = 'karolin'
s2 = 'kerstin' def hamming_distance(s1, s2):
b1, b2 = bytearray(s1, encoding='utf8'), bytearray(s2, encoding='utf8')
diff = 0
for i in range(len(b1)):
if b1[i] != b2[i]:
diff += bin(b1[i] ^ b2[i]).count("1")
return diff print(hamming_distance(s1, s2))

  C/C++:

#include <iostream>
#define ull unsigned long long
int hamming_distance(ull x, ull y)
{
int dist = 0;
ull or = x ^ y;
while (or)
{
dist++; or &= or - 1;
}
return dist;
}
int main()
{
ull x, y;
while (std::cin >> x >> y)
std::cout << "最小汉明距离为:" << hamming_distance(x, y) << std::endl;
return 0;
}

  以C代码为例,比如两个整数:1, 2,他们的二进制分别为 001, 010 ,从 001 → 010 最少需要2步替换,也就是最小汉明距离为2;又比如两整数:12, 34,他们的二进制分别为 001100, 100010,001100 → 100010 的最小汉明距离为4。

  运行示例:

  注:第三个可以自己动手验证一下~

  然后维基上还有一段代码:

int hamming_distance(unsigned x, unsigned y)
{
return __builtin_popcount(x ^ y);
}
//if your compiler supports 64-bit integers
int hamming_distance(unsigned long long x, unsigned long long y)
{
return __builtin_popcountll(x ^ y);
}

  好像我的电脑上不行,没有__builtin_popcountll()函数。

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. 12.Hamming Distance(汉明距离)

    Level:   Easy 题目描述: The Hamming distance between two integers is the number of positions at which th ...

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

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

随机推荐

  1. 【AHOI2005】约数研究

    发现luogu的UI改版后AC以后不能给题目评定难度了…… P1403 [AHOI2005]约数研究 类似素数筛的一道题,不过是约数. 先顺手写了个暴力做法,TLE定了~ #include<bi ...

  2. hdu1716 排列2

    12  21 123 132  213 231 321 312 .... 每次都将后面n-1位进行全排列.递归的出口当起始坐标等于终止坐标时.需要还原. 设计标记数组.因为需要从小到大输出. #def ...

  3. 01-Java基本语法【前言、入门程序、常量、变量】

    重点知识记录: 1.java语言是美国Sun公司在1995年推出的高级编程语言. 2.java语言主要应用在互联网程序的开发领域. 3.二进制转换 1)十进制数据转换成二进制数据:使用除以2获取余数的 ...

  4. 《CSS揭秘》》

    1,透明边框 默认状态下,背景会延伸到边框区域的下层.这样就在半透明的黑色边框中透出了这个容器自己的纯白色背景. 谢天谢地,从w3c的背景与边框第三版开始,我们可以通过 background-clip ...

  5. thinkphp中简单的控制器使用

    1.在路由(route.php)中定义一条路由 Route::rule('new/:name/:id','index/News/read'); 2.在index下的controller控制器中新建一个 ...

  6. CSS遮罩层简易写法

      现在很多站点弹出框,都需要一个遮罩层.写法很多,以下是我比较喜欢的一种: .box{ position:absolute; top:0; bottom:0; left:0; right:0; ba ...

  7. Git的基本使用 -- 远程仓库

    SSH公钥 生成公钥 ssh-keygen -t rsa -C "xxx@xxx.com" 然后按三次回车 添加公钥 cat ~/.ssh/id_rsa.pub查看公钥 将生成的公 ...

  8. python下matplotlib、numpy、pandas联合作图逐步深入分析

    1.代码1: from pandas import Series,DataFrame from numpy.random import randn import numpy as np import ...

  9. 题解【洛谷P2323】 [HNOI2006]公路修建问题

    题面 题解 跑两遍\(Kruskal\),第一次找出\(k\)条一级公路,第二次找出\(n - k - 1\)条二级公路,直接计算\(MST\)的权值之和即可. 代码 #include <ios ...

  10. JavaScript错误-throw、try和catch

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. finally 语句在 try 和 catch 语句之后,无论是否有触发异常,该语句都会执行. JavaS ...