Skip to content

Commit bbead63

Browse files
committed
counting bits solution
1 parent 012343e commit bbead63

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

counting-bits/hyer0705.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
function countBits(n: number): number[] {
4+
const ans: number[] = Array.from({ length: n + 1 }, () => 0);
5+
6+
for (let i = 1; i <= n; i++) {
7+
ans[i] = ans[i >> 1] + (i & 1);
8+
}
9+
10+
return ans;
11+
}

0 commit comments

Comments
 (0)