forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountingBits.kt
44 lines (39 loc) · 979 Bytes
/
CountingBits.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given an integer `n`, return an array `ans` of length `n + 1` such that for each `i` (0 <= i <= n),
* `ans[i]` is the number of 1's in the binary representation of `i`.
*
* [Source](https://leetcode.com/problems/counting-bits/)
*/
@UseCommentAsDocumentation
private fun countBits(n: Int): IntArray {
val ans = IntArray(n + 1) { 0 }
(0..n).forEach {
ans[it] = countBitsOnDigit(it)
}
return ans
}
private fun countBitsOnDigit(n: Int): Int {
var count = 0
var num = n
while (num != 0) {
if (num.and(1) == 1) count++ // get LSB
num = num.shr(1)
}
return count
}
fun main() {
// 0 --> 0
// 1 --> 1
// 2 --> 10
countBits(n = 2) shouldBe intArrayOf(0, 1, 1)
// 0 --> 0
// 1 --> 1
// 2 --> 10
// 3 --> 11
// 4 --> 100
// 5 --> 101
countBits(n = 5) shouldBe intArrayOf(0, 1, 1, 2, 1, 2)
}