461. Hamming Distance
题目大意: 求两个数字的汉明距离,汉明距离是指两个数字对应位不同的数量,详情见示例
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:
解题思路:通过异或运算^
将对应位不同的数值标示出来,使用 Integer.bitCount() 方法返回二进制1
的数量
Last updated
Was this helpful?