1、题目描述
2、问题分析
使用C++ 标准库中的 bitset 类,将整数转换为二进制形式,然后再将其转换为字符串,最后比较字符串。
3、代码
int hammingDistance(int x, int y) { bitset<> a(x);
bitset<> b(y); string a_s = a.to_string();
string b_s = b.to_string(); string::iterator it1 = a_s.begin() ;
string::iterator it2 = b_s.begin() ;
int re = ;
while( it1 != a_s.end() && it2 != b_s.end() ){
if( *it1 != *it2 ){
re++;
}
it1++;
it2++;
}
return re;
}