Skip to content

Commit 7dda6db

Browse files
committed
solved leetcode/leetcoding_challenge/2023/nov2023/week5/number_of_1_bits.cpp
(cherry picked from commit 2c2974e)
1 parent 5d84008 commit 7dda6db

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <algorithm>
2+
#include <cstdint>
3+
#include <vector>
4+
#include <queue>
5+
#include <set>
6+
#include <limits>
7+
#include <map>
8+
#include <unordered_set>
9+
#include <unordered_map>
10+
#include <iterator>
11+
#include <sstream>
12+
#include <iostream> // includes cin to read from stdin and cout to write to stdout
13+
using namespace std; // since cin and cout are both in namespace std, this saves some text
14+
15+
class Solution {
16+
public:
17+
int hammingWeight(uint32_t n) {
18+
int count = 0;
19+
while (n != 0) {
20+
if (n % 2 != 0) {
21+
count += 1;
22+
}
23+
n >>= 1;
24+
}
25+
return count;
26+
}
27+
};
28+
29+
int main() {
30+
Solution soln;
31+
32+
cout << soln.hammingWeight(00000000000000000000000000001011) << endl;
33+
34+
return 0;
35+
}

0 commit comments

Comments
 (0)