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 < 2^31 .
Example:
Input: x = 1, y = 4
Output: 2
Explanation:
1 | 1 (0 0 0 1) |
The above arrows point to positions where the corresponding bits are different.
思路
题目说 y < 2^31 那写一个32次的循环,依次统计一共有多少个1就好。
代码
1 | //C++ |
然后看到别人思路貌似有个比32次循环更快的方法:
1 | //C++ |