The title

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

character The numerical
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

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

In Roman numerals, the smaller number is usually placed to the right of the larger number. But there are exceptions. For example, instead of writing IIII for 4, I would write IV. The number 1 is to the left of the number 5, which is equal to the large number 5 and the number 4 is obtained by reducing the number 1. Similarly, the number 9 is represented as IX. This particular rule only applies in 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. Make sure the input is in the range 1 to 3999.

Example 1:

Input: "III" Output: 3Copy the code

Example 2:

Input: "IV" Output: 4Copy the code

Example 3:

Input: "IX" Output: 9Copy the code

Example 4:

Input: "LVIII" Output: 58Copy the code

L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV" Output: 1994Copy the code

Explanation: M = 1000, CM = 900, XC = 90, IV = 4

Tip:

  • 1 <= s.length <= 15
  • S contains only characters (‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’)
  • The topic data guarantees that s is a valid Roman numeral and that the integer is in the range [1, 3999]
  • All the test cases are in accordance with the rules of Roman numerals.
  • Examples like IL and IM don’t fit the problem. 49 should be XLIX, 999 should be CMXCIX.
  • For detailed rules for writing Roman numerals, see Mathematics.

implementation

Methods a

    public int romanToInt1(String s) {
        int result = 0;
        Map<Character,Integer> map = new HashMap<>();
        // Store the mapping between Roman letters and Arabic numerals
        map.put('I'.1);
        map.put('V'.5);
        map.put('X'.10);
        map.put('L'.50);
        map.put('C'.100);
        map.put('D'.500);
        map.put('M'.1000);
        // Iterates through the input string, subtracting the input string if it is smaller than the input string if it is smaller than the input string
        for (int i = 0; i < s.length(); i++) {
            if (i < s.length()-1 && map.get(s.charAt(i)) < map.get(s.charAt(i+1))){
                result -= map.get(s.charAt(i));
            }else{ result += map.get(s.charAt(i)); }}return result;
    }
Copy the code

Method 2

    public int romanToInt2(String s) {
        int result = 0;
        Map<Character,Integer> map = new HashMap<>();
        // Replace the special case
        s = s.replace("IV"."a");
        s = s.replace("IX"."b");
        s = s.replace("XL"."c");
        s = s.replace("XC"."d");
        s = s.replace("CD"."e");
        s = s.replace("CM"."f");
        // Store the mapping between Roman letters and Arabic numerals
        map.put('I'.1);
        map.put('V'.5);
        map.put('X'.10);
        map.put('L'.50);
        map.put('C'.100);
        map.put('D'.500);
        map.put('M'.1000);
        // Special case replacement
        map.put('a'.4);
        map.put('b'.9);
        map.put('c'.40);
        map.put('d'.90);
        map.put('e'.400);
        map.put('f'.900);

        for (int i = 0; i < s.length(); i++) {
            result += map.get(s.charAt(i));
        }
        return result;
    }
Copy the code