leetcode 461. 汉明距离

很简单的位运算的计算,不多说了,solution前面几行是c++的输入输出加速,不必理会。

static int x = [](){

    ios::sync_with_stdio(false);

    cin.tie(0);

    return 0;

}();

class Solution {

public:

    int hammingDistance(int x, int y) {

        int ret = 0;

        for(int i = 0; i < 32; i++){

            ret += (x & 1) ^ (y & 1);

            x=x>>1;y=y>>1;

        }

        return ret;

    }

};



猜你喜欢

转载自blog.csdn.net/torch_man/article/details/80564597