1. Description
2. Solution
- 32 times
class Solution {
public:
int hammingDistance(int x, int y) {
int m = x ^ y;
int distance = 0;
while(m) {
distance = m & 1;
m = m >> 1;
}
return distance;
}
};
- n times(n is the number of 1)
class Solution {
public:
int hammingDistance(int x, int y) {
int m = x ^ y;
int distance = 0;
while(m) {
m = m & (m - 1);
distance ;
}
return distance;
}
};