Skip to content

Commit 8cea40d

Browse files
authored
Easy 191. Number of 1 Bits
1 parent b971000 commit 8cea40d

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

191. Number of 1 Bits

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
int hammingWeight(uint32_t n) {
4+
int count =0;
5+
6+
//****Approach 1****
7+
// while( n!= 0){
8+
// count += (n%2); // Remainder Count... easy Binary count
9+
// n >>=1;
10+
// }
11+
// return count;
12+
13+
//*****Approach 2*****
14+
// while(n>0){
15+
// n = (n&(n-1)); // Bit Manipulation
16+
// count++;
17+
// }
18+
// return count;
19+
20+
//*****Approach 3*****
21+
while(n !=0){
22+
count += (n%2); //Remainder Count... easy Binary count
23+
n = n/2;
24+
}
25+
return count;
26+
}
27+
};

0 commit comments

Comments
 (0)