[Algorithm solution] LeetCode 13. Roman numerals to integers

topic

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.
The 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 means two parallel ones. 12 is written as XII, which means X + II. 27 is written as XXVII, which is XX + V + II.

Normally, the small numbers in Roman numerals are to the right of the large 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 the number represented is equal to the number 4 obtained by subtracting the number 1 from the large number 5. Similarly, 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. Ensure that the input is in the range of 1 to 3999.

Source: LeetCode
Link: https://leetcode-cn.com/problems/roman-to-integer

Solution one

In addition to the 7 single-character numbers, there are 6 double-character numbers. Maintain these 13 situations in a Map. To traverse the Roman numerals, first judge in the map based on two characters, if not, judge based on one character.

class Solution {
    public int romanToInt(String s) {
        Map<String,Integer> numMap = new HashMap<String,Integer>(16);
        numMap.put("M",1000);
        numMap.put("CM",900);
        numMap.put("D",500);
        numMap.put("CD",400);
        numMap.put("C",100);
        numMap.put("XC",90);
        numMap.put("L",50);
        numMap.put("XL",40);
        numMap.put("X",10);
        numMap.put("IX",9);
        numMap.put("V",5);
        numMap.put("IV",4);
        numMap.put("I",1);

        int result = 0;
        for(int i = 0; i < s.length(); i++) {
            if(i < s.length() -1 && numMap.containsKey(s.substring(i,i + 2))) {
                result += numMap.get(s.substring(i,i + 2));
                i++;
            } else {
                result += numMap.get(s.substring(i,i + 1));
            }
        }
        return result;
    }
}

Solution two

Judge character by character, and add up each result. If the previous character is smaller than the current character, it means that the previous character has been added twice, and the number of the previous character code needs to be subtracted twice.

class Solution {
    public int romanToInt(String s) {
        int result = 0,prev = 1001;
        for(int i = 0; i < s.length(); i++) {
            int num = getValue(s.charAt(i));
            result += num;
            if(num > prev) {
                result -= 2 * prev;
            }
            prev = num;
        }
        return result;
    }

    public int getValue(char c) {
        switch (c) {
            case 'I': return 1;
            case 'V': return 5;
            case 'X': return 10;
            case 'L': return 50;
            case 'C': return 100;
            case 'D': return 500;
            case 'M': return 1000;
        }
        return 0;
    }
}

Guess you like

Origin blog.csdn.net/vxzhg/article/details/106673540