| Topic | Difficulty | Companies |
|---|---|---|
| Mathematical Algorithms | MEDIUM | Amazon Microsoft Facebook |
Given a string s representing a roman numeral. Convert s into an integer.
Problem Note
s is guaranteed to be within the range from 1 to 3999.SYMBOL VALUE
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
III in Roman numeral, just three ones added together. Eleven is written as, XI, which is simply X + I. The number fifty-six is written as LVI, which is just L + V + I.IIII. Instead, the number four is written as IV. We see that the one is before the five, so we subtract one from five to make it four. The same principle applies to the number forty, which is written as XL. There are six instances where subtraction is used:Example 1
Input: "VII"
Output: 7
Explanation: V+I+I = 5+1+1 = 7
Example 2
Input: "IV"
Output: 4
Explanation: The value of I is 1 which is smaller than the value of V i.e. 5. So, we subtract I from V. V-I = 5-1 = 4
Example 3
Input: "XC"
Output: 90
Explanation: The value of X is 10 which is smaller than the value of C i.e. 100. So, we subtract X from C. C-X = 100-10 = 90
Example 4
Input: "XVII"
Output: 17
Explanation: X = 10, V= 5, II = 2.