
liuchuo.net
柳婼 の blog – 我不管,反正我最萌~LeetCode 461. Hamming Distance. The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Input: x = 1, y = 4. 1 (0 0 0 1). 4 (0 1 0 0). The above arrows point to positions where the corresponding bits are different. 对于右移一位,采用x / 2和 y / 2的方式,对于比较最后一位,即比较x % 2 和 y % 2,统计不相同的次数cnt,直到x和y都等于0为止. Class Solution { public: int hammingDistance(int x, int y) { int cnt = 0; while(x! 0) { if(x % 2!
http://www.liuchuo.net/