zoukankan      html  css  js  c++  java
  • [LeetCode#13] Roman to Integer

    Problem:

    Given a roman numeral, convert it to an integer.

    Input is guaranteed to be within the range from 1 to 3999.

    Analysis:

    This problem actually requires us to follow a given pattern to implement code.
    Except some coding skills, the algorithm has already be given. 
    
    Reference to Roman Numerals:
    https://en.wikipedia.org/wiki/Roman_numerals
    
    The max number the roman numeral stystem can represent is 3999. 
    'I' can represent range [1, 3]
    add 'V' can represent range [4, 8]
    
    Unlike the numerial system, every position get number from [0, 9], but the weight for those position is differnt. 
    99, the 9 at '10' digit means 90, however at '1' digit means 9.
    
    The Roman numeral system exclude such representation. It represents number through "different characters" and relative position. 1. different characters. 
    ('I', 1); ('V', 5); ('X', 10); ('L', 50); ('C', 100); ('D', 500); ('M', 1000);
    
    2. represent through characters combination for each digit in number. 
    1954 as [M][CM][L][IV]
    The character's value not based on position, but on itself!!!
    
    What's more, the character is arranged from [high digit to low digit (in number form)]!
    
    Note:
    Each '1(000)' character could appear consecutively three times. the '5' character are not appear consecutively. 
    Thus for each digit, we have following way to construct that digt, let us use digit 1 as example.
    --------------------------------------------------------------------------------------------
    I (1)
    II (1+1)
    III (1+1+1)
    IV  (5-1)
    V   (5)
    VI  (5+1)
    VII (5+2)
    VIII (5+3)
    IX  (10-1)
    X   (10)
    --------------------------------------------------------------------------------------------
    Except 'IV' and 'IX', we could directly add the value of each character. Thus we could take this pattern into our programm!!!
    for (int i = 0; i < s.length(); i++) {
        int cur = map.get(s.charAt(i));
        ret += cur;
        if (i >= 1) {
            int pre = map.get(s.charAt(i-1));
            if (pre*5 == cur || pre*10 == cur)
                ret -= 2*pre;
        }
    }
    
    Note: only when "if (pre*5 == cur || pre*10 == cur)"!!!
    For each digit representation, only 3 characters could be appeared.
    1 digit: I , V , X
    10 digit: X, L, C
    100 digit: C, D, M
    1000 digit: M (that's why the maximum value is roman numerial system is : 3999)

    Solution:

    public class Solution {
        public int romanToInt(String s) {
            if (s == null || s.length() == 0)
                throw new IllegalArgumentException("The passed in arguments is illegal!");
            HashMap<Character, Integer> map = new HashMap<Character, Integer> ();
            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);
            int ret = 0;
            for (int i = 0; i < s.length(); i++) {
                int cur = map.get(s.charAt(i));
                ret += cur;
                if (i >= 1) {
                    int pre = map.get(s.charAt(i-1));
                    if (pre*5 == cur || pre*10 == cur)
                        ret -= 2*pre;
                }
            }
            return ret;
        }
    }
  • 相关阅读:
    [Python] Marshmallow QuickStart
    [Python]Marshmallow 代码
    [python]Flask-migrate简单入门
    [数据库]Sqlite使用入门
    [Python] dict对象的keys()和values()返回的值,是否总是保证一一对应?
    【Weiss】【第03章】练习3.20:中缀表达式转后缀表达式
    【Weiss】【第03章】练习3.19:计算后缀表达式
    【Weiss】【第03章】练习3.18:检查平衡符号
    【Weiss】【第03章】练习3.17:懒惰删除
    【TIJ4】第六章全部习题【习题未完成】
  • 原文地址:https://www.cnblogs.com/airwindow/p/4777472.html
Copyright © 2011-2022 走看看