56. Old Numerals to Number
An archive stores page numbers in an old numeral system. Its symbols are I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. Symbols are normally written from largest to smallest and their values are added. The exception is subtraction: a symbol placed directly before a larger one is subtracted, which is allowed only for I before V or X, X before L or C, and C before D or M.
Given a valid numeral s, return the whole number it stands for.
Example 1
- Input:
- s = "XIV"
- Output:
- 14
- Explanation:
X is 10 and IV is 4, so the value is 14.
Example 2
- Input:
- s = "CDXLIV"
- Output:
- 444
- Explanation:
CD is 400, XL is 40 and IV is 4, giving 444.
Constraints
1 ≤ s.length ≤ 15
s is a valid numeral for a number from 1 to 3999
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(1)