191. Number of 1 Bits

问题描述

解决方案

//8ms
class Solution {
public:
    int hammingWeight(uint32_t n) {
        bitset<32> bi(n);
        return bi.count();
    }
};


//4ms
// class Solution {
// public:
//     int hammingWeight(uint32_t n) {
//         int count=0;
//         while(n!=0)
//         {
//             n=n&(n-1);
//             ++count;
//         }
//         return count;
//     }
// };
posted @ 2016-08-23 22:08  弦断  阅读(101)  评论(0编辑  收藏  举报