Leetcode: 13-Roman Numeral to Integer

13.Roman numerals to integers

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 1. 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 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.

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.

Problem-solving ideas

这道题两年前做过,已经觉得很陌生
问题不要复杂化,常规想,常规做

给一串罗马数字,是由一个个字符构成
1)考虑在没有特殊情况下的时候,每个字符对应着一个数字
2)将出现的特殊情况特殊处理,需要注意的是,如果遇到两个字符开头的,
第一个是以特殊字符开头与另外一个不构成6个特殊情况之一,需要额外处理。

Code

class Solution {
    
    
    public int romanToInt(String s) {
    
    
        char[] chars = s.toCharArray();
		
		存储key-value
        Map<Character, Integer> maps = new HashMap();
        maps.put('I', 1);
        maps.put('V', 5);
        maps.put('X', 10);
        maps.put('L', 50);
        maps.put('C', 100);
        maps.put('D', 500);
        maps.put('M', 1000);
        int sum = 0;
		
		//遍历
        for (int i = 0; i < chars.length; i++) {
    
    
         	//特殊情况处理  
            if (chars[i] == 'I' || chars[i] == 'X' || chars[i] == 'C') {
    
    
                if (i+1 < chars.length && chars[i + 1] == 'V'  && chars[i] == 'I') {
    
    
                    sum += 4;
                    i++;
                    continue;
                } else if (i+1 < chars.length  && chars[i + 1] == 'X'  && chars[i] == 'I') {
    
    
                    sum += 9;
                    i++;
                    continue;
                } else if (i+1 < chars.length && chars[i + 1] == 'L' && chars[i] == 'X') {
    
    
                    sum += 40;
                    i++;
                } else if (i+1 < chars.length && chars[i + 1] == 'C' && chars[i] == 'X') {
    
    
                    sum += 90;
                    i++;
                } else if (i+1 < chars.length  && chars[i + 1] == 'D' && chars[i] == 'C') {
    
    
                    sum += 400;
                    i++;
                    continue;
                } else if (i+1 < chars.length  && chars[i + 1] == 'M' && chars[i] == 'C') {
    
    
                    sum += 900;
                    i++;
                    continue;
                } else {
    
    
                    sum += maps.get(chars[i]);
                    continue;
                }
            } else {
    
    
                sum += maps.get(chars[i]);
            }
        }
        return sum;
        }
    }

testInsert picture description here

Guess you like

Origin blog.csdn.net/JAYU_37/article/details/107226473