Problema
Convert a valid Roman numeral to its value. Symbols are I=1, V=5, X=10, L=50, C=100, D=500, M=1000; a smaller symbol before a larger one is subtracted (IV = 4, CM = 900).
Implement roman_to_int(text).
Input
text: A valid Roman numeral.
Output
Return its value.
Restricciones
1 ≤ value ≤ 3,999
Ejemplos
Entrada:
text = "XIV"
Salida esperada:
expected = 14
Entrada:
std::string text = "XIV";
Salida esperada:
long long expected = 14;
Entrada:
string text = "XIV";
Salida esperada:
long expected = 14;
Explicación
X = 10, IV = 4: 14.
Entrada:
text = "MCMXCIV"
Salida esperada:
expected = 1994
Entrada:
std::string text = "MCMXCIV";
Salida esperada:
long long expected = 1994;
Entrada:
string text = "MCMXCIV";
Salida esperada:
long expected = 1994;
Explicación
M=1000, CM=900, XC=90, IV=4: 1994.
Cómo abordarlo
Inténtalo primero por tu cuenta y luego abre las pistas una a una.
Pista 1
Most symbols add their value, but some pairs subtract when a smaller symbol appears before a larger one.
Pista 2
Process the numeral from left to right and compare each symbol with the next one.
Pista 3
Make sure the last symbol is always handled correctly since it has no follower.
Complejidad esperada
O(n) time, O(1) extra space
Referencias
Este reto está adaptado para CodeUtility. Sigue el enunciado y las restricciones indicadas arriba.