File tree 2 files changed +21
-0
lines changed
2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 169
169
326|[ Power of Three] ( ./0326-power-of-three.js ) |Easy|
170
170
328|[ Odd Even Linked List] ( ./0328-odd-even-linked-list.js ) |Medium|
171
171
334|[ Increasing Triplet Subsequence] ( ./0334-increasing-triplet-subsequence.js ) |Medium|
172
+ 338|[ Counting Bits] ( ./0338-counting-bits.js ) |Easy|
172
173
342|[ Power of Four] ( ./0342-power-of-four.js ) |Easy|
173
174
344|[ Reverse String] ( ./0344-reverse-string.js ) |Easy|
174
175
345|[ Reverse Vowels of a String] ( ./0345-reverse-vowels-of-a-string.js ) |Easy|
You can’t perform that action at this time.
0 commit comments