Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
private val i = arrayOf("", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX")
private val x = arrayOf("", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC")
private val c = arrayOf("", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM")
private val m = arrayOf("", "M", "MM", "MMM")
fun intToRoman(num: Int): String {
return m[num / 1000] + c[(num % 1000) / 100] + x[(num % 100) / 10] + i[num % 10]
}
}
class Solution {
private val i = arrayOf(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1)
private val r = arrayOf("M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I")
fun intToRoman(num: Int): String {
val string = StringBuilder()
var index = 0
var n = num
while (n > 0) {
while (n >= i[index]) {
n -= i[index]
string.append(r[index])
}
index++
}
return string.toString()
}
}