Roman Numeral to Integer
Given a string numeral representing a Roman numeral, convert it to an integer.
Roman numerals contain the symbols representing values in the following list:
"I" = 1"V" = 5"X" = 10"L" = 50"C" = 100"D" = 500"M" = 1000
Roman numerals are typically written largest to smallest, from left to right, and can be computed by summing up the values of all the symbols. However, in some cases, when a symbol of lower value is to the left of a symbol of higher value, then the lower value is subtracted from the higher one.
There are 6 cases where this is possible:
- When
"I"is before"V", we get4. - When
"I"is before"X", we get9. - When
"X"is before"L", we get40. - When
"X"is before"C", we get90. - When
"C"is before"D", we get400. - When
"C"is before"M", we get900.
Constraints
1 ≤ n ≤ 15wherenis the length ofnumeral1 ≤ k ≤ 3000wherekis the numbernumeralrepresents
https://binarysearch.com/problems/Roman-Numeral-to-Integer
Examples
Example 1
Input
- numeral =
XII
Output
- answer =
12
Explanation
"XII" = 10 + 1 + 1 = 12
Example 2
Input
- numeral =
XIV
Output
- answer =
14
Explanation
"XIV" = 10 + 4 = 14
Leave a comment