We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 4fd0e7a commit 0f53f94Copy full SHA for 0f53f94
number-of-1-bits/nrudev.ts
@@ -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