Skip to content

Commit 70b6181

Browse files
committedJan 14, 2025
Add solution #338
1 parent 2bcc4ff commit 70b6181

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
326|[Power of Three](./0326-power-of-three.js)|Easy|
170170
328|[Odd Even Linked List](./0328-odd-even-linked-list.js)|Medium|
171171
334|[Increasing Triplet Subsequence](./0334-increasing-triplet-subsequence.js)|Medium|
172+
338|[Counting Bits](./0338-counting-bits.js)|Easy|
172173
342|[Power of Four](./0342-power-of-four.js)|Easy|
173174
344|[Reverse String](./0344-reverse-string.js)|Easy|
174175
345|[Reverse Vowels of a String](./0345-reverse-vowels-of-a-string.js)|Easy|

‎solutions/0338-counting-bits.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* 338. Counting Bits
3+
* https://leetcode.com/problems/counting-bits/
4+
* Difficulty: Easy
5+
*
6+
* Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n),
7+
* ans[i] is the number of 1's in the binary representation of i.
8+
*/
9+
10+
/**
11+
* @param {number} n
12+
* @return {number[]}
13+
*/
14+
var countBits = function(n) {
15+
const result = new Array(n + 1).fill(0);
16+
for (let i = 0; i <= n; i++) {
17+
result[i] = result[i >> 1] + (i & 1);
18+
}
19+
return result;
20+
};

0 commit comments

Comments
 (0)
Please sign in to comment.