LC013 - Roman to Integer
Problem
You get a string (array of chars) of Roman numerics and have to convert it into an integer.
Example
Input: s = “XVII”
Output: 17
Glossary
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
I can be placed before V and X to make 4 and 9. X can be placed before L and C to make 40 and 90. C can be placed before D and M to make 400 and 900.
Solution
Naive
Use an if-else or match case to check the value of the char at the current string index, then check the char value at the next index if the current index is I, X, or C.
The time complexity of this implementation is , and there is no additional memory usage.
Map
This has the same performance as the previous version, just more concise and readable
Last updated