forked from realpacific/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertToHex.kt
50 lines (46 loc) · 1.38 KB
/
ConvertToHex.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
45
46
47
48
49
50
package questions
import _utils.UseCommentAsDocumentation
import utils.shouldBe
/**
* Given an integer num, return a string representing its hexadecimal representation.
* For negative integers, two’s complement method is used.
* All the letters in the answer string should be lowercase characters,
* and there should not be any leading zeros in the answer except for the zero itself.
* [Source](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) – [solution](https://leetcode.com/problems/convert-a-number-to-hexadecimal/discuss/824192/Java-100-Time-with-Detailed-Explanation)
*/
@UseCommentAsDocumentation
private fun toHex(num: Int): String {
if (num in 0..9) {
return "$num"
}
val hexMap = mapOf<Int, Char>(
10 to 'a',
11 to 'b',
12 to 'c',
13 to 'd',
14 to 'e',
15 to 'f'
)
var temp = num
var result = ""
for (i in 1..8) {
if (temp == 0) {
break
}
val last4bits = temp.and(0b1111)
if (last4bits <= 9) {
result = last4bits.toString() + result
} else {
result = hexMap[last4bits].toString() + result
}
temp = temp.shr(4)
}
return result
}
fun main() {
toHex(26) shouldBe "1a"
toHex(27) shouldBe "1b"
toHex(10) shouldBe "a"
toHex(1) shouldBe "1"
toHex(-1) shouldBe "ffffffff"
}