Skip to content

Commit 1d41918

Browse files
author
eunhwa99
committed
Counting Bits
1 parent 0b1f1dc commit 1d41918

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

β€Žcounting-bits/eunhwa99.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
Β (0)