利用按位异或操作^
来标记不同的位置。
c++
class Solution {
public:
int hammingDistance(int x, int y) {
int tmp = x^y;
int res = 0;
while (tmp) {
if ((tmp>>1)<<1!=tmp) {
res++;
}
tmp>>=1;
}
return res;
}
};
Hamming Distance
Copyright © 2017 Powered by LZH, Theme used GitHub CSS.