LeetCode刷题--461.Hamming Distance
从这个博客开始打算坚持在LeetCode上刷一点算法题,之后可能也会有Python版本的现在还是用C++在写了,就先从简单题开始刷起了.
题目以及理解
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:
1 | Input: x = 1, y = 4 |
理解
很简单的题目,就是问两个数字的二进制有几位是不一样的.那就直接对两个数进行异或再判断有几位1就可以了.
代码
1 | class Solution { |