Number of 1 Bits Solutions in C++
Number 191
Difficulty Easy
Acceptance 50.0%
Solutions
C++ solution by haoel/leetcode
class Solution {
public:
int hammingWeight(uint32_t n) {
int cnt = 0;
for(;n>0; n/=2){
if (n & 0x1) cnt++;
}
return cnt;
}
};
C++ solution by liuyubobobo/Play-Leetcode
#include <iostream>
using namespace std;
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
res += (n & 1);
n >>= 1;
}
return res;
}
};
int main() {
return 0;
}
C++ solution by liuyubobobo/Play-Leetcode
#include <iostream>
using namespace std;
class Solution {
public:
int hammingWeight(uint32_t n) {
int res = 0;
while(n){
n &= (n - 1)
res ++;
}
return res;
}
};
int main() {
return 0;
}