461.汉明距离

在这里插入图片描述

思路:
依次记录两个数字每位是否相同,如果不同,计数器加一。

class Solution {
public:
    int hammingDistance(int x, int y) {
        int count=0;
        while(x||y)
        {
            if(x%2!=y%2) ++count;
            x/=2;
            y/=2;
        }
        return count;
    }
};

在这里插入图片描述

发布了90 篇原创文章 · 获赞 7 · 访问量 2162

猜你喜欢

转载自blog.csdn.net/weixin_43784305/article/details/103355172