#13

Roman to Integer

pupil · 320 · lc easy +21 · verified · 66.3% accepted · 17,550 likes · top 71%

play →

Description

Roman numerals use seven symbols: I=1, V=5, X=10, L=50, C=100, D=500, M=1000. A symbol of lesser value immediately before one of greater value is subtracted; otherwise it is added. Convert the given Roman numeral string to its integer value.

Example 1:

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

Example 2:

Input: s = "III"
Output: 3
Explanation: III = 3.

Example 3:

Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 4:

Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Code

1
2
3