[LeetCode]13. Roman numeral to integer

Keywords: Algorithm leetcode

Title Description  

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

Character           numerical 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 parallel ones. 12 is written as XII, which is X + II. 27 write do   27. XX + V + II.

Usually, the small Roman numerals are to the right of the large ones. But there are also special cases. For example, 4 is not written as IIII, but IV. The number 1 is on the left of the number 5, and the number represented is equal to the value 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is represented as IX. This special rule applies only to the following six cases:

    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.

Example 1:

Input: s = "III"
Output: 3

Example 2:

Input: s = "IV"
Output: 4

Example 3:

Input: s = "IX"
Output: 9

Example 4:

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

Example 5:

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

Tips:

    1 <= s.length <= 15
    s contains only characters ('I ',' V ',' x ',' l ',' C ','d','m ')
    The title data ensures that s is a valid Roman numeral and represents an integer in the range [1, 3999]
    The test cases given in the title comply with the Roman numeral writing rules, and there will be no cross position and so on.
    Examples such as IL and IM do not meet the title requirements. 49 should write XLIX and 999 should write CMXCIX.
    For detailed writing rules of Roman numerals, please refer to Roman numerals - Mathematics.

Source: LeetCode
Link: https://leetcode-cn.com/problems/roman-to-integer
The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

thinking

It mainly deals with six special rules, and the combination of special rules is a combination of two digits. It is not a special rule. You only need to retrieve the corresponding Arabic values of Roman numerals and accumulate the corresponding Arabic numerals.

code implementation

class Solution {
    public int romanToInt(String s) {
        char[] romanNumCharArray = s.toCharArray();
        int length = romanNumCharArray.length;
        if (length < 0 || length > 15) {
            return -1;
        }

        int num = 0;
        char[] romanNumeralssLib = new char[]{'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int[] arabNumberLib = new int[]{1, 5, 10, 50, 100, 500, 1000};

        for (int i = 0; i < length; i++) {
            char currRoman = romanNumCharArray[i];

            //6 special rule processing
            //I can be placed to the left of V (5) and X (10) to represent 4 and 9.
            if (currRoman == 'I' && i + 1 < length && romanNumCharArray[i + 1] == 'V') {
                num += 4;
                //Special rule combinations account for two places
                i++;//Push back 2 bits (here, the for subscript moves by 1 bit, and i itself will accumulate 1, pushing back and moving 2 bits in total)
                continue;
            }
            if (currRoman == 'I' && i + 1 < length && romanNumCharArray[i + 1] == 'X') {
                num += 9;
                i++;
                continue;
            }
            //X can be placed to the left of L (50) and C (100) to represent 40 and 90.  
            if (currRoman == 'X' && i + 1 < length && romanNumCharArray[i + 1] == 'L') {
                num += 40;
                i++;
                continue;
            }
            if (currRoman == 'X' && i + 1 < length && romanNumCharArray[i + 1] == 'C') {
                num += 90;
                i++;
                continue;
            }
            //C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
            if (currRoman == 'C' && i + 1 < length && romanNumCharArray[i + 1] == 'D') {
                num += 400;
                i++;
                continue;
            }
            if (currRoman == 'C' && i + 1 < length && romanNumCharArray[i + 1] == 'M') {
                num += 900;
                i++;
                continue;
            }

            //Retrieve the Arabic values corresponding to Roman numerals and accumulate the corresponding numbers
            for (int j = 0; j < romanNumeralssLib.length; j++) {
                if (currRoman == romanNumeralssLib[j]) {
                    num += arabNumberLib[j];
                    break;
                }
            }
        }


        return num;
    }
}

Execution result: passed
Execution time: 4 ms, beating 98.11% of users in all Java submissions
Memory consumption: 38.4 MB, beating 88.93% of users in all Java submissions
Pass test case: 3999 / 3999

Record and summary, Wednesday, November 10, 2021 03:38:54 CST.

Posted by Guernica on Wed, 10 Nov 2021 00:35:53 -0800