Hamming Distance 是指两个字符串之间的汉明距离是指两个相等长度的字符串,对应位置上不同字符的个数。
例如:
The Hamming distance between 1011101 and 1001001 is 2.
The Hamming distance between 2143896 and 2233796 is 3.
The Hamming distance between "toned" and "roses" is 3.
1011101 与 1001001 之间的汉明距离是 2。
2143896 与 2233796 之间的汉明距离是 3。
"toned" 与 "roses" 之间的汉明距离是 3。
应用十进制转换成二进制的方法解决该问题。
讲解Python主函数什么时候用
http://www.360doc.com/content/14/1023/17/3200886_419259481.shtml
python 整除运算符 //
Problem Statement
(Source) The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, calculate the Hamming distance.
Note:
0 ≤ x
, y
<
231
.
Example:
Input: x = 1, y = 4 Output: 2 Explanation: 1 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.
Python Solution
^运算 按位异或 异或运算满足相同为0,不同为1,正好可以将bit位上不同的标为1,结果表示为整数。
利用python内置函数bin
转换一个整数
x
为二进制的字符串表示。
内置函数count() 方法用于统计字符串里某个字符出现的次数。