leetcode_Hamming Distance

在信息论中,两个等长字符串之间的海明距离是两个字符串对应位置的不同字符的个数。

class Solution {
public:
    int hammingDistance(int x, int y) {
        int dst=0, n = x ^ y; //异或操作,求出不同字符串的位置
        while (n){
            ++dst;
            n &=n - 1; //去掉位数最右边的1
        }
        return dst;
    }
};


猜你喜欢

转载自blog.csdn.net/helei001/article/details/77862001