Skip to content

Commit 0f53f94

Browse files
author
Heeseung Koo
committed
w03: 191. Number of 1 Bits
1 parent 4fd0e7a commit 0f53f94

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

number-of-1-bits/nrudev.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function hammingWeight(n: number): number {
2+
const binary: string = getBinary(n, "");
3+
return binary.replaceAll("0", "").length;
4+
}
5+
6+
function getBinary(n: number, s: string): string {
7+
if (n <= 1) return n + s;
8+
return getBinary(Math.trunc(n / 2), (n % 2) + s);
9+
}

0 commit comments

Comments
 (0)