看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. centos 7安装jdk8

    前提 执行安装的当前用户为root 下载安装包 现在oracle官网下载jdk需要登录才可以下载,故下载安装包比较麻烦.下载地址: http://www.oracle.com/technetwork/ ...

  2. c数据结构 -- 线性表之 复杂的链式存储结构

    复杂的链式存储结构 循环链表 定义:是一种头尾相接的链表(即表中最后一个结点的指针域指向头结点,整个链表形成一个环) 优点:从表中任一节点出发均可找到表中其他结点 注意:涉及遍历操作时,终止条件是判断 ...

  3. XSS Payload List

    标签.事件.属性 xss的攻击原理就是前端被插入了恶意的js代码,下面展示大部分可以执行js的标签.事件.属性: 标签(label) <script> <a> <p> ...

  4. Tomcat创建项目

    查看项目信息 index.jsp默认首页 更新资源自动部署不用重启服务器,要用debug的方式启动 更新java代码和更新资源自动部署不用重启服务器,要用debug的方式启动

  5. spring(三):BeanFactory

  6. axios中then不用第二个参数,最好用catch

    一般来说,不要在then方法里面定义 Reject 状态的回调函数(即then的第二个参数),总是使用catch方法. // bad promise .then(function(data) { // ...

  7. .net c# MVC提交表单的4种方法

    https://blog.csdn.net/qingkaqingka/article/details/85047781

  8. Stream中的map

    #map可以让一个对象A的流转换为宁外一种对象B的流(其实也是A对象元素组成的流) 1.对象转换为List集合 //若Eticket是一个对象,其中orderId是String类型 //eticket ...

  9. 【C语言】用C语言输出一个吃豆人

    大圆盘减去扇形和小圆盘: #include <math.h> #include <stdio.h> int main() { double x, y; ; y >= -; ...

  10. 机器学习(ML)十六之目标检测基础

    目标检测和边界框 在图像分类任务里,我们假设图像里只有一个主体目标,并关注如何识别该目标的类别.然而,很多时候图像里有多个我们感兴趣的目标,我们不仅想知道它们的类别,还想得到它们在图像中的具体位置.在 ...