Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer.
Example 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
Example 2:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
Example 3:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Constraints:
- 1 <= s.length <= 15
- s contains only the characters (‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’).
- It is guaranteed that s is a valid roman numeral in the range [1, 3999].
Approach 1
class Solution {
private val map = mapOf(
'I' to 1,
'V' to 5,
'X' to 10,
'L' to 50,
'C' to 100,
'D' to 500,
'M' to 1000
)
fun romanToInt(s: String): Int {
var pre = 0
return s.fold(0) { acc, c ->
val curr = map.getValue(c)
val more = if (curr > pre) 2 * pre else 0
pre = curr
acc + curr - more
}
}
}
Approach 2
/**
* 找一个数字 n,让所有罗马数字字符对其取模使得所有余数都不相同,
* 这样我们就可以使用 IntArray 来映射罗马数字字符和其对应的值,数组映射比 map 会更省更快。
* IntArray 的 size 不需要等于 n,取罗马数字字符中最大余数 + 1 即可,这样可以节省空间。
*
* 如下方法用于提前找出模 mod、数组 size 以及数组映射值:
* fun main() {
* val romans = arrayOf('I', 'V', 'X', 'L', 'C', 'D', 'M')
* val mod = (2..26).first { n ->
* romans.map { it.code % n }.toSet().size == romans.size
* }
* val size = romans.maxOf { it.code % mod } + 1
* println("mod=$mod size=$size") // mod=14 size=13
* val values = IntArray(size)
* mapOf(
* 'I' to 1,
* 'V' to 5,
* 'X' to 10,
* 'L' to 50,
* 'C' to 100,
* 'D' to 500,
* 'M' to 1000
* ).forEach { (c, v) -> values[c.code % mod] = v }
* values.joinToString().apply(::println) // 0, 0, 5, 1, 10, 0, 50, 1000, 0, 0, 0, 100, 500
* }
*/
class Solution {
private val values = intArrayOf(0, 0, 5, 1, 10, 0, 50, 1000, 0, 0, 0, 100, 500)
fun romanToInt(s: String): Int {
var pre = 0
return s.fold(0) { acc, c ->
val curr = values[c.code % 14]
val more = if (curr > pre) 2 * pre else 0
pre = curr
acc + curr - more
}
}
}
IntArray 的 size 还可以更省空间,取罗马数字字符余数中余数最大值减去最小值再加 1。不过这样的话用余数取 index 的时候还需要做偏移(偏移值即为余数最小值):
class Solution {
private val values = intArrayOf(5, 1, 10, 0, 50, 1000, 0, 0, 0, 100, 500)
fun romanToInt(s: String): Int {
var pre = 0
return s.fold(0) { acc, c ->
val curr = values[c.code % 14 - 2]
val more = if (curr > pre) 2 * pre else 0
pre = curr
acc + curr - more
}
}
}