Skip to content

Commit b300fb0

Browse files
committed
Feat: add solution for 191
1 parent 2ee18ce commit b300fb0

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

number-of-1-bits/HC-kang.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
191. Number of 1 Bits
3+
4+
Example 1:
5+
Input: n = 11
6+
Output: 3
7+
Explanation:
8+
The input binary string 1011 has a total of three set bits.
9+
10+
Example 2:
11+
Input: n = 128
12+
Output: 1
13+
Explanation:
14+
The input binary string 10000000 has a total of one set bit.
15+
16+
Example 3:
17+
Input: n = 2147483645
18+
Output: 30
19+
Explanation:
20+
The input binary string 1111111111111111111111111111101 has a total of thirty set bits.
21+
*/
22+
23+
function hammingWeight(n: number): number {
24+
return n.toString(2).split('1').length - 1;
25+
26+
// let count = 0;
27+
// while (n !== 0) {
28+
// count += n & 1;
29+
// n >>>= 1;
30+
// }
31+
// return count;
32+
}

0 commit comments

Comments
 (0)