leetcode:Number of 1 Bits

1:

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n)
        {
            if(n&0x1==1)
            {
                count++;
            }
            n=n>>1;
        }
        return count;
    }
};
:2:

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n)
        {
            count++;
            n=(n-1)&n;
        }
        return count;
    }
};


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章