美文网首页
13 Roman to Integer

13 Roman to Integer

作者: yangminz | 来源:发表于2018-07-16 00:05 被阅读10次

    title: Roman to Integer
    tags:
    - roman-to-integer
    - No.13
    - simple
    - finite-automata
    - amortized-analysis
    - naive
    - integer


    Problem

    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 a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 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: C = 100, L = 50, XXX = 30 and III = 3.
    

    Example 5:

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

    Solution

    Naive

    For value v_{i} and v_{i+1}, if v_{i} is mis-added, then just minus 2 * v_{i} to correct it. Running time is O(log(x)).

    import java.util.*;
    
    class Solution {    
        public int romanToInt(String s) {
            
            Map<String, Integer> f = new HashMap<String, Integer>();
            f.put("I", 1);
            f.put("V", 5);
            f.put("X", 10);
            f.put("L", 50);
            f.put("C", 100);
            f.put("D", 500);
            f.put("M", 1000);
    
            String[] x = s.split("");
            int      i = 0;
            int      y = f.get(x[i]);
            
            for ( ; i<s.length()-1; i++) {
                if (f.get(x[i]) >= f.get(x[i+1])) {y = y + f.get(x[i+1]);}
                else                              {y = y + f.get(x[i+1]) - 2 * f.get(x[i]);}
            }
            
            return y;
        }
    }
    

    Finite Automata

    Many character string problems can be solved by finite automata. For any state, we define its state transition function f(s_i, x_j) = s_k and amortized value v(s_i, x_j):

    State Transition Table

    I V X L C D M
    0 1 0 2 0 3 0 0
    1 0 0 0 4 4 4 4
    2 1 0 0 0 0 4 4
    4 1 0 2 0 3 0 0

    Here [4] is the error state which is not processed in the algorithm.

    Amortized Value Transition Table

    I V X L C D M
    0 1 5 10 50 100 500 1000
    1 1 3 8 0 0 0 0
    2 1 5 10 30 80 0 0
    4 1 5 10 50 100 300 800

    Running time is O(x) with space complexity O(56).

    import java.util.*;
    
    class Solution {    
        public int romanToInt(String s) {
            // state transition table
            int[][] f = new int[][] {
                {1,0,2,0,3,0,0},
                {0,0,0,4,4,4,4},
                {1,0,0,0,0,4,4},
                {1,0,2,0,3,0,0}
            };
            
            // amortized value transition table
            int[][] v = new int[][] {
                {  1,  5, 10, 50,100,500,1000},
                {  1,  3,  8,  0,  0,  0,  0 },
                {  1,  5, 10, 30, 80,  0,  0 },
                {  1,  5, 10, 50,100,300,800 }
            };
            
            // map from roman char to index
            Map<String, Integer> indexMap = new HashMap<String, Integer>();
            indexMap.put("I", 0);
            indexMap.put("V", 1);
            indexMap.put("X", 2);
            indexMap.put("L", 3);
            indexMap.put("C", 4);
            indexMap.put("D", 5);
            indexMap.put("M", 6);
            
            int      s = 0;
            int      y = 0;
            int      k = 0;
            String[] x = s.split("");
            for (int i=0; i<s.length(); i++) {
                k = indexMap.get(x[i]);
                
                // update
                y = y + v[s][k];
                s = f[s][k];
            }
            
            return y;
        }
    }
    

    相关文章

      网友评论

          本文标题:13 Roman to Integer

          本文链接:https://www.haomeiwen.com/subject/klvmpftx.html