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 fbcc68a commit 3bdee5aCopy full SHA for 3bdee5a
counting-bits/byol-han.js
@@ -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