class Solution {
public:
int hammingWeight(uint32_t n) {
n = ((n & 0xAAAAAAAA) >> 1) + (n & 0x55555555);
n = ((n & 0xCCCCCCCC) >> 2) + (n & 0x33333333);
n = ((n & 0xF0F0F0F0) >> 4) + (n & 0x0f0f0f0f);
n = ((n & 0xff00ff00) >> 8) + (n & 0x00ff00ff);
n = ((n & 0xffff0000) >> 16) + (n & 0xffff);
return (int)n;
}
};
leetcode Number of 1 Bits
最新推荐文章于 2020-11-06 22:12:43 发布