Roman to Integer
Convert a Roman numeral string to an integer.
I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Subtractive: IV=4, IX=9, XL=40, XC=90, CD=400, CM=900.
Example
III
3
- Input:
sis a Roman numeral written as a string of symbols, hereIII: three copies of the symbol I, each worth . - Math: a numeral's value is the total of its symbols' values, except inside the six subtractive pairs from the description (IV, IX, XL, XC, CD, CM), where the pair as a whole stands for the difference, e.g. IV .
IIIcontains no V, X or any larger symbol, so none of those pairs can occur and every symbol simply adds: .- Output: the plain integer
3, not a string. - Notice that changing the last symbol to V gives IIV, which is not valid; the valid way to write is IV, where the I now counts as instead of .
Constraints
- 1 <= len(s) <= 15
- s is a valid Roman numeral in [1, 3999]