class Solution { public: int hammingDistance(int x, int y) { int res = x^y; // 异或不同为结果为1 unsigned int flag = 1; int count = 0; while(flag){ if(flag&res) // 判断哪一位为1 count++; flag = flag<<1; } return count; } };
2024-01-19 16:27:28
class Solution { public: int hammingDistance(int x, int y) { int res = x^y; // 异或不同为结果为1 unsigned int flag = 1; int count = 0; while(flag){ if(flag&res) // 判断哪一位为1 count++; flag = flag<<1; } return count; } };