Skip to content

Latest commit

 

History

History
19 lines (19 loc) · 491 Bytes

hanmingdistance.md

File metadata and controls

19 lines (19 loc) · 491 Bytes

题目描述:
image
解决过程:
用的0到31位注意比较解决的,看了题解,直接对x ^ y进行求其二进制中1的数量
代码:

class Solution {
public:
    int hammingDistance(int x, int y) {
        int bit = 0, ret = 0;
        while (bit < 32) {
            int _bit = 1 << bit;
            ret += ((x & (_bit)) ^ (y & (_bit))) != 0;
            bit++;
        }
        return ret;
    }
};