[leetcode]191. Number of 1 Bits
Analysis
周五啦~—— [高考结束了耶~]
Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).
比较简单,就是把十进制转换成二进制,记录一下1的个数就好了。
Implement
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
int t = n % 2;
if(t == 1)
res++;
n /= 2;
}
return res;
}
};