LeetCodeOJ_191_Number of 1 Bits_e

答題鏈接




題目:

For example, the 32-bit integer ’11' has binary representation00000000000000000000000000001011, so the function should return 3.


代碼:

<span style="font-size:14px;">class Solution {
public:
    int hammingWeight(uint32_t n) {
        int num=0;
        while(n!=0)
        {
           if(n&1==1)
              num++;
          n=n/2;
        }
        return num;
    }
};
</span>



結果:



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