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 ...
随机推荐
- codeblocks汉化
1.- 汉化包 腾讯微云 2.首先打开codeblocks安装文件夹,如:D:\CodeBlocks\share\CodeBlocks\locale\zh_CN,注意:zh_CN文件夹需自行创建 3. ...
- Wx-mpvue开发小程序
一.准备 安装Node 安装vue-cli ( npm install --global vue-cli ) 二.创建 初始化项目 ( vue init mpvue/mpvue-quickstart ...
- python接口自动化测试 - requests库的基础使用
简单介绍 requests库简单易用的HTTP库 Get请求 格式: requests.get(url) 注意:若需要传请求参数,可直接在 url 最后的 ? 后面,也可以调用 get() 时多加一个 ...
- 概率dp poj 2151
题意: 这道题目的意思很简单,有t个ACM队,m个题目,题目给出了每个队对每个题目做出的概率大小(0到1之间,包含0和1),要求每个队至少做出一道题(签到题),同时,要求获胜队必须至少能够做出n道题( ...
- 委托与事件--delegate&&event
委托 访问修饰符 delegate 返回值 委托名(参数); public delegate void NoReturnNoPara(); public void NoReturnNoParaMeth ...
- 论STA | SOCV / POCV 之 variation (2)
芯片制造涉及到许多复杂重复的过程,如:光刻.蚀刻.离子注入.扩散.退火.而且都是原子级操作,尽管控制非常严格,但偏差不可避免. 工艺偏差会导致芯片物理参数偏差,如:线宽.沟道掺杂浓度.线厚.临界尺寸. ...
- 题解 SP19148【INS14G - Kill them All】
SP19148[INS14G - Kill them All] 前置知识:组合数 乘法逆元 感觉其他博客讲的不是很清楚,也没有说组合数公式是怎么来的,我这样数论极菜的萌新看了好久才想明白qwq.. 还 ...
- sublime不支持ascill编码办法
1.按下组合键ctrl+shift+p,输入:install package,回车 2.在弹出的安装包框中搜索:ConvertToUTF8或者GBK Encoding Support,选择点击安装: ...
- linux 配置php环境变量
vim /etc/profile //加上 export PATH=$PATH:/usr/local/php/bin 保存退出 source /etc/profile php -v 注:该配置对所有用 ...
- 关于jsp的action如何调用servlet的自定义方法
一.起因: 希望将同属于某个模块的简单功能整合到一起,不创建太多的servlet 二.问题描述: action或者method属性是否能直接调用自定义方法 三.补充知识点: 查询得知:servelet ...