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 0b1f1dc commit 1d41918Copy full SHA for 1d41918
βcounting-bits/eunhwa99.kt
@@ -0,0 +1,29 @@
1
+class Solution {
2
+ /*
3
+ 0 -> 0
4
+ 1 -> 1
5
+ 2 -> 1
6
+ 3 -> 2
7
+ 4 -> 1
8
+ 5 -> 2
9
+ 6 -> 2
10
+ 7 -> 3
11
+ 8 -> 1
12
+ 9 -> 2
13
+ 10 -> 2
14
+ 11 -> 3
15
+ 12 -> 2
16
+ 2,4,8,16,.. -> 1
17
+ μ§μ: i/2μ 1μ κ°μ
18
+ νμ: i/2μ 1μ κ°μ + 1
19
+ */
20
+ // TC: O(n)
21
+ // SC: O(n)
22
+ fun countBits(n: Int): IntArray {
23
+ val result = IntArray(n + 1)
24
+ for(i in 0..n) {
25
+ result[i] = result[i / 2] + (i % 2)
26
+ }
27
+ return result
28
29
+}
0 commit comments