美文网首页
02_汉明距离

02_汉明距离

作者: butters001 | 来源:发表于2019-11-06 11:43 被阅读0次
class Solution(object):
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
        x = bin(x)[2:]
        y = bin(y)[2:]
        if len(x) > len(y):
            short, long = y, x
        else:
            short, long = x, y

        while len(short) != len(long):
            short = '0' + short

        count = 0
        for index in range(len(short)):
            if short[index] != long[index]:
                count += 1

        return count


# leetcode 最优解
class Solution2(object):
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
        return bin(x ^ y).count('1')

相关文章

  • 02_汉明距离

  • 汉明距离、超立方体、异或的一些知识

    汉明距离和汉明重量 汉明距离是以理查德·卫斯里·汉明的名字命名的。在信息论中,两个等长字符串之间的汉明距离是两个字...

  • LeetCode 461.汉明距离

    ?博客原文 :《LeetCode 461.汉明距离 - JavaScript》 汉明距离定义:两个整数之间的汉明距...

  • 汉明距离

  • 汉明距离

    两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 给出两个整数 x 和 y,计算它们之间的汉明...

  • 汉明距离

    指的是两个(相同长度)字符串,你变成我,我变成你,需要换掉多少个字符的总和,即Max(Sum1,Sum2),比如...

  • 汉明距离

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/hamm...

  • 汉明距离

    https://zhuanlan.zhihu.com/p/94081111pHash简单来说,是通过感知哈希算法对...

  • 汉明距离

    题目: 题目的理解: 将整数转化为二进制,然后再转化为字符串,进行字符串比较,得到不同的位数。 python实现 ...

  • 汉明距离

    两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目。 给出两个整数 x 和 y,计算它们之间的汉明...

网友评论

      本文标题:02_汉明距离

      本文链接:https://www.haomeiwen.com/subject/upcubctx.html