Convert Roman numerals to integers (C++/Python)

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, the Roman numeral 2 is written as II, which is two 1's side by side. 12 is written as XII, which is X + II. 27 is written as XXVII, which is XX + V + II.

Typically, smaller numbers in Roman numerals are to the right of larger numbers. But there are special cases. For example, 4 is not written as IIII, but as IV. The number 1 is to the left of the number 5 and represents a number equal to the large number 5 minus the number 1, which is the value 4. Likewise, the number 9 is represented as IX. This special rule only applies to the following six situations:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.
X can be placed to the left of L (50) and C (100) to represent 40 and 90.
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Given a Roman numeral, convert it to an integer. Make sure the input is in the range 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58 Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90, IV = 4.

hint:

1 <= s.length <= 15
s contains only characters ('I', 'V', 'X', 'L', 'C', 'D', 'M') question data to ensure that s is a
valid Roman numerals, and indicates that the integer is within the range [1, 3999].
The test cases given in the question all comply with the writing rules of Roman numerals, and there will be no overlapping situations.
Examples such as IL and IM do not meet the question requirements. 49 should be written as XLIX and 999 should be written as CMXCIX.
For detailed writing rules of Roman numerals, please refer to Roman Numerals-Mathematics.

This problem can be easily solved using map, which can be solved from left to right or from left to left again.

C++ (from left to right)

class Solution {
    
    
private:
    unordered_map<char,int>symbolValues = {
    
    
        {
    
    'I',1},
        {
    
    'V',5},
        {
    
    'X',10},
        {
    
    'L',50},
        {
    
    'C',100},
        {
    
    'D',500},
        {
    
    'M',1000},
    };

public:
    int romanToInt(string s) {
    
    
        int ans = 0;
        int n = s.length();
        for(int i = 0; i < n; ++i){
    
    
            int value = symbolValues[s[i]];
            if(i < n - 1 && value < symbolValues[s[i+1]] ){
    
    
                ans -= value;
            }else{
    
    
                ans += value;
            }
        }
        return ans;
    }
};

python3 (right to left)

class Solution:
    def romanToInt(self, s: str) -> int:
        mapping = {
    
    
            'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000
        }
        highestLevel = 1
        result = 0
        for ch in s[::-1]:
            level = mapping[ch]
            if level >= highestLevel:
                result += level
                highestLevel = level
            else:
                result -= level
        return result

Guess you like

Origin blog.csdn.net/smile66688/article/details/120356185