leetcode 461. 汉明距离

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

static int x = [](){

   ios::sync_with_stdio(false);

    cin.tie(0);

   return0;

}();

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;

    }

};




版权声明:本文为Torch_Man原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。