13_Roman to Integer
[easy]
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Roman rules:
M = 1000
D = 500
C = 100
L = 50
X = 10
V = 5
I = 1
Special cases to pay attention:
CM = 900
CD = 400
XC = 90
XL = 40
IX = 9
IV = 4
Idea
Store the roman rules in a dictionary, whose keys are the letters and values are the corresponding numbers.
Loop over each letter in the string except the last one. If the current number is less than the next, then subtract this number. Otherwise add this number.
Add the last number.
Complexity
Time:
Space:
Solution
Last updated