Skip to content

Commit 63753a2

Browse files
authored
Merge pull request #1631 from byol-han/main
[byol-han] WEEK 14 solutions
2 parents 5d41a1c + 3bdee5a commit 63753a2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

counting-bits/byol-han.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://leetcode.com/problems/counting-bits/submissions/1681218194/
3+
* @param {number} n
4+
* @return {number[]}
5+
*/
6+
var countBits = function (n) {
7+
const ans = [];
8+
for (let i = 0; i <= n; i++) {
9+
// Convert i to binary with i.toString(2)
10+
// Count 1s by splitting into chars, filtering '1', and getting length
11+
const binary = i.toString(2);
12+
const onesCount = binary.split('').filter((bit) => bit === '1').length;
13+
ans.push(onesCount);
14+
}
15+
return ans;
16+
};

0 commit comments

Comments
 (0)