LeetCode461

  本题注意点:

      1、位运算及十进制转二级制:这个不难想到;

      2、对字符串进行字符统计:str.count(),原来用循环实现,可读性差。

import numpy as np

def hammingDistance(x, y):
    """
    :type x: int
    :type y: int
    :rtype: int
    """
    t = bin(x ^ y)
    return t.count("1")   # 字符串.count()

    # version2:
    # cnt = 0
    # for i in range(2, len(t)):
    #     if t[i] == '1':
    #         cnt += 1
    # return cnt

# sample:
x, y = 1, 4
print(hammingDistance(x, y))

猜你喜欢

转载自blog.csdn.net/zuocuomiao/article/details/82054306