标签:rip style 没有 com strong 一个 target sel targe
1、链接地址
https://leetcode.com/problems/hamming-distance/description/
2、题目要求
汉明距离指两个整数的二进制表示中,对应位置数字不同的位数
注意:两个整数的范围是[0,2**31)
示例:
输入:x=1,y=4
输出:2
解释:
1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑
1和4的二进制如上,箭头指示的两个地方二进制位是不同的,总共有两个地方,所以输出2
3、解答
1 class Solution: 2 def hammingDistance(self, x, y): 3 """ 4 :type x: int 5 :type y: int 6 :rtype: int 7 """ 8 xor = x ^ y 9 count = bin(xor).count("1") 10 11 return count
4、官方解答
暂时还没有给出官方答案
5、注意事项
(1)x^y执行异或操作,最终结果为1的二进制位表示该位置是不同的,但是整个结果是一个十进制数字
(2)将上面得到的十进制数字执行bin操作,得到"0b101"字符串表示的二进制
(3)使用字符串的count方法可以得到某个字符的统计值
标签:rip style 没有 com strong 一个 target sel targe
原文地址:https://www.cnblogs.com/hzerdudu/p/9492592.html