Roman to Integer
Roman numerals use seven symbols:
| Symbol | I | V | X | L | C | D | M |
|---|---|---|---|---|---|---|---|
| Value | 1 | 5 | 10 | 50 | 100 | 500 | 1000 |
Symbols are written from largest to smallest and added up, so XVI is 10 + 5 + 1 = 16. Six pairs subtract instead: IV = 4, IX = 9, XL = 40, XC = 90, CD = 400 and CM = 900, so XIV is 14 and MCMXC is 1990.
Given a valid Roman numeral s, return its value.
Constraints
- 1 ≤ length of
s≤ 15 sis a valid Roman numeral for a number from 1 to 3999.
Example 1
Input
s = "III"
Output
3
Why:
1 + 1 + 1.
Example 2
Input
s = "LVIII"
Output
58
Why:
L = 50, V = 5, III = 3.
Example 3
Input
s = "MCMXCIV"
Output
1994
Why:
M = 1000, CM = 900, XC = 90, IV = 4.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –