hamming distance(汉明距离)
看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(汉明距离)的更多相关文章
- [LeetCode] Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [LeetCode] 461. Hamming Distance 汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 461. Hamming Distance(汉明距离)
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- 477 Total Hamming Distance 汉明距离总和
两个整数的 汉明距离 指的是这两个数字的二进制数对应位不同的数量.计算一个数组中,任意两个数之间汉明距离的总和.示例:输入: 4, 14, 2输出: 6解释: 在二进制表示中,4表示为0100,14表 ...
- [LeetCode] Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Swift]LeetCode461. 汉明距离 | Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- [Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
- Leetcode#461. Hamming Distance(汉明距离)
题目描述 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入: x = ...
- 12.Hamming Distance(汉明距离)
Level: Easy 题目描述: The Hamming distance between two integers is the number of positions at which th ...
- [LeetCode] 477. Total Hamming Distance 全部汉明距离
The Hamming distance between two integers is the number of positions at which the corresponding bits ...
随机推荐
- 压缩包安装mysql8.0
在使用django的时候遇到一个错误,就是用脚本改变数据库的时候,发现mysql的版本不够,需要的版本应该大于5.8,而我的只有5.5,就很烦,恰好我之前有8.0的压缩包.(mysql重装已经不下十次 ...
- JS jQuery 点击页面漂浮出文字
看到有些网站点击页面任意地方都会弹出文字出来 感觉很炫酷 但其实实现方法很简单 哇哈哈哈~~~ // 调用 ( e, 消失毫秒, 数组, 向上漂浮距离) $(document).click(funct ...
- JavaScript 开胃菜
注释 单行注释:// 快捷键: CTRL + / 多行注释: /* 内容 */ 快捷键: ctrl + shift + / 变量 申明变量 var name; 赋值 name = 'peach'; 初 ...
- 从bbs.3dmgame.com与qq的登录解析oauth2.0协议
点击3dm上的qq图标,浏览器跳转到,地址为: https://graph.qq.com/oauth2.0/show ?which=Login &display=pc &respons ...
- 解决修改JDK环境变量不生效方法
解决修改JDK环境变量不生效方法 brupsuit1.7在安装时一直报错jdk版本低,我就将jdk1.6版本的卸了换成1.8的,结果修改了环境变量但它一直给我不生效.... 1.之前版本未卸载干净 进 ...
- 概念理解_L2范数(欧几里得范数)
L1范数 L1范数是指向量中各个元素绝对值之和 L2范数 L2范数.欧几里得范数一些概念. 首先,明确一点,常用到的几个概念,含义相同. 欧几里得范数(Euclidean norm) ==欧式长度 = ...
- web自动化环境搭建(python+selenium+webdriver)
本文档以谷歌浏览器为例,故自动化测试环境为下: 自动化工具为:selenium+webdriver 脚本语言为:Python3.X 浏览器:Chrome 系统环境:Win10 编译工具:Pycharm ...
- Iris_xorm
xorm表基本操作及高级操作 表结构基本操作 对表结构的操作最常见的操作是查询和统计相关的方法,我们首先来看相关实现: 条件查询 Id值查询:参数接收主键字段的值.例如: var user User ...
- Iris_cookie和session
3. Session的使用和控制 在实际的项目开发中,我们会经常有业务场景使用到Session功能.在iris框架中,也为我们提供了方便使用,功能齐全的Session模块.Session模块的源码目录 ...
- TXT文件也能挂木马
什么?TXT文件也能挂马?是的!TXT文件不仅有挂马的危险,而且有时候可能非常的危险!不过,严格说来,应该给这个所谓的"TXT"文件加个引号,因为它们是看起来是TXT文件,实则是隐 ...