Problem Description
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(1000) to make 400 and 900.
Given a Roman numeral, convert it to an integer.
Examples
Example 1: Input: s = "III" Output: 3 Explanation: III = 3. Example 2: Input: s = "LVIII" Output: 58 Explanation: L = 50, V = 5, III = 3. Example 3: Input: s = "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Python Solution
class Solution:
def romanToInt(self, s: str) -> int:
roman_values = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
}
result = 0
prev_value = 0
for char in reversed(s):
curr_value = roman_values[char]
if curr_value >= prev_value:
result += curr_value
else:
result -= curr_value
prev_value = curr_value
return result
Java Solution
class Solution {
public int romanToInt(String s) {
Map romanValues = new HashMap<>();
romanValues.put('I', 1);
romanValues.put('V', 5);
romanValues.put('X', 10);
romanValues.put('L', 50);
romanValues.put('C', 100);
romanValues.put('D', 500);
romanValues.put('M', 1000);
int result = 0;
int prevValue = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int currValue = romanValues.get(s.charAt(i));
if (currValue >= prevValue) {
result += currValue;
} else {
result -= currValue;
}
prevValue = currValue;
}
return result;
}
}
C++ Solution
class Solution {
public:
int romanToInt(string s) {
unordered_map romanValues = {
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}
};
int result = 0;
int prevValue = 0;
for (int i = s.length() - 1; i >= 0; i--) {
int currValue = romanValues[s[i]];
if (currValue >= prevValue) {
result += currValue;
} else {
result -= currValue;
}
prevValue = currValue;
}
return result;
}
};
JavaScript Solution
/**
* @param {string} s
* @return {number}
*/
var romanToInt = function(s) {
const romanValues = {
'I': 1, 'V': 5, 'X': 10, 'L': 50,
'C': 100, 'D': 500, 'M': 1000
};
let result = 0;
let prevValue = 0;
for (let i = s.length - 1; i >= 0; i--) {
const currValue = romanValues[s[i]];
if (currValue >= prevValue) {
result += currValue;
} else {
result -= currValue;
}
prevValue = currValue;
}
return result;
};
C# Solution
public class Solution {
public int RomanToInt(string s) {
var romanValues = new Dictionary {
{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50},
{'C', 100}, {'D', 500}, {'M', 1000}
};
int result = 0;
int prevValue = 0;
for (int i = s.Length - 1; i >= 0; i--) {
int currValue = romanValues[s[i]];
if (currValue >= prevValue) {
result += currValue;
} else {
result -= currValue;
}
prevValue = currValue;
}
return result;
}
}
Complexity Analysis
- Time Complexity: O(n) where n is the length of the input string
- Space Complexity: O(1) as we only use a constant amount of extra space
Solution Explanation
This solution uses a right-to-left approach:
- We create a map of Roman numeral values
- We process the string from right to left
- For each character:
- If the current value is greater than or equal to the previous value, we add it
- If the current value is less than the previous value, we subtract it
- We update the previous value for the next iteration
Key points:
- Processing from right to left makes it easier to handle subtraction cases
- We don't need to look ahead to determine if we should subtract
- We handle all special cases (IV, IX, etc.) automatically
- The solution is efficient as we only need to traverse the string once