原题:
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
x
andy
, 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.
哈明距离就是两个二进制数每一位进行对比,不同的位数。方法是不断对比最后一位然后数字右移一位继续对比,以此类推直到两个数都为零。结果:
Success
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Hamming Distance.
Memory Usage: 8.1 MB, less than 95.45% of C++ online submissions for Hamming