Skip to content

Commit b2ce37e

Browse files
committed
solve: Week 03 number of 1 bits
1 parent 3475a97 commit b2ce37e

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

number-of-1-bits/eunice-hong.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function hammingWeight(n: number): number {
2+
// 1. convert the number to a binary string
3+
const binaryString = n.toString(2);
4+
5+
// 2. count the number of 1s in the binary string
6+
return binaryString.split("").filter((char) => char === "1").length;
7+
}

0 commit comments

Comments
 (0)