Leetcode P0013"Roman to Integer" 题解

2020/08/18 Leetcode

博文中会简要介绍Leetcode P0013题目分析及解题思路。

“Roman to Integer”是一道比较简单的题目,这题考察的主要是纯数学和能否通过映射操作来优化代码结构,和上一题考察的内容相仿。

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

以下是Java的题解代码实现。

class Solution {
    public int romanToInt(String s) {
        Map<String, Integer> symbol2IntMap = new HashMap<>();
        // Mapping
        symbol2IntMap.put("I", 1);
        symbol2IntMap.put("IV", 4);
        symbol2IntMap.put("V", 5);
        symbol2IntMap.put("IX", 9);
        symbol2IntMap.put("X", 10);
        symbol2IntMap.put("XL", 40);
        symbol2IntMap.put("L", 50);
        symbol2IntMap.put("XC", 90);
        symbol2IntMap.put("C", 100);
        symbol2IntMap.put("CD", 400);
        symbol2IntMap.put("D", 500);
        symbol2IntMap.put("CM", 900);
        symbol2IntMap.put("M", 1000);
        
        String[] arr = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int num = 0, arrItr = 0, romanItr = 0;
        while (romanItr < s.length()) {
            if (romanItr+arr[arrItr].length() > s.length()) {
                ++arrItr;
                continue;
            }    
            
            String currStr = s.substring(romanItr, romanItr+arr[arrItr].length());
            if (currStr.equals(arr[arrItr])) {
                num += symbol2IntMap.get(arr[arrItr]);
                romanItr += arr[arrItr].length();
            }
            else {
                ++arrItr;
            }
        }
        
        return num;
    }
}

以下是C++的题解代码实现。

class Solution {
public:
    int romanToInt(string s) {
        // Mapping
        // unordered_map<string, int> symbol2int_map;
        
        string arr[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        int num = 0, arr_itr = 0, roman_itr = 0;
        while (roman_itr < s.length()) {
            if (roman_itr+arr[arr_itr].length() > s.length()) {
                ++arr_itr;
                continue;
            }    
            
            string curr_str = s.substr(roman_itr, arr[arr_itr].length());
            if (curr_str == arr[arr_itr]) {
                num += symbol2int_map[arr[arr_itr]];
                roman_itr += arr[arr_itr].length();
            }
            else {
                ++arr_itr;
            }
        }
        
        return num;
    }
};

Search

    Table of Contents