Skip to content

Commit 71307c3

Browse files
committed
Add week 2 solutions: counting-bits
1 parent c6d37af commit 71307c3

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

counting-bits/gitsunmin.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
function countBits(n: number): number[] {
7+
const arr = new Array(n + 1).fill(0);
8+
for (let i = 1; i <= n; i++) arr[i] = arr[i >> 1] + (i & 1);
9+
return arr;
10+
}

0 commit comments

Comments
 (0)