leetcode 461.Hamming Distance python
461.Hamming Distance --leetcode
很久没有编程了, 想锻炼锻炼自己的编程能力, 准备开始刷些编程题, 以前学得那点C已经忘得差不多了, 所以准备用python编, 之后可能会有C++或者Java的编程解法, 不过其实leetcode上有各种语言的解法, 很多都写的好棒…
题目
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.
思路
题目说的很清楚了,计算汉明距离的话, 对两个字符串进行异或运算,并统计结果为1的个数,这就是汉明距离
python解法
1 | class Solution(object): |