leetcode-461. 汉明距离

 

 

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;
    }
};

 

posted @ 2021-07-25 14:52  三一一一317  阅读(35)  评论(0)    收藏  举报