191. Number of 1 Bits
做题历程:
- 2016/9/21.本题之前做过不止1次,本次耗时3分钟,独立解出
本题难度还是相对偏低的,而且我貌似也没有用最精秒的解法,只是用了常规解法。 就是不断的左移,然后取最右位的值即可。
代码如下:
class Solution {
public:
int hammingWeight(uint32_t n) {
unsigned int result = 0;
for (int i = 0; i < 32; ++i) {
result += (0x01 & n);
n >>= 1;
}
return result;
}
};