leecode第四百六十一题(汉明距离)

class Solution {
public:
    int hammingDistance(int x, int y) {
        int num=x^y;//问题转化为检测x^y中1的个数
        int res=0;
        
        while(num!=0)
        {
            num=num&(num-1);
            res++;
        }
        
        return res;
    }
};

分析:

同剑指offer那道题。

posted @ 2019-08-05 11:30  深夜十二点三十三  阅读(89)  评论(0编辑  收藏  举报