https://leetcode-cn.com/problems/hamming-distance/
- 自己的解答
class Solution {
public int hammingDistance(int x, int y) {
String s = Integer.toBinaryString(x ^ y);
int res=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='1')res++;
}
return res;
}
}
- 移位
class Solution {
public int hammingDistance(int x, int y) {
int xor = x ^ y;
int res=0;
while (xor!=0){
if(xor%2==1){
res++;
}
xor>>=1;
}
return res;
}
}