LeetCode刷题--461.Hamming Distance

从这个博客开始打算坚持在LeetCode上刷一点算法题,之后可能也会有Python版本的现在还是用C++在写了,就先从简单题开始刷起了.

题目以及理解

题目链接-461. Hamming Distance

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
2
3
4
5
6
7
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就可以了.

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
int hammingDistance(int x, int y) {
int z = x ^ y;
int c = 0;
while (z > 0){
if ((z & 1) == 1)
c++;
z >>= 1;
}
return c;
}
};