Hamming Distance

2019-05-25 22:56:29 浏览数 (1)

1. Description

2. Solution

  • 32 times
代码语言:javascript复制
class Solution {
public:
    int hammingDistance(int x, int y) {
        int m = x ^ y;
        int distance = 0;
        while(m) {
            distance  = m & 1;
            m = m >> 1;
        }
        return distance;
    }
};
  • n times(n is the number of 1)
代码语言:javascript复制
class Solution {
public:
    int hammingDistance(int x, int y) {
        int m = x ^ y;
        int distance = 0;
        while(m) {
            m = m & (m - 1);
            distance  ;
        }
        return distance;
    }
};

0 人点赞