package fundamentals.string.convert.lc013_romantointeger;

import java.util.HashMap;
import java.util.Map;

/**
 * Given a roman numeral, convert it to an integer.
 * Input is guaranteed to be within the range from 1 to 3999.
 */
public class Solution {

    private static Map<Character,Integer> roman = new HashMap<>();

    static {
        roman.put('I', 1);
        roman.put('V', 5);
        roman.put('X', 10);
        roman.put('L', 50);
        roman.put('C', 100);
        roman.put('D', 500);
        roman.put('M', 1000);
    }

    public int romanToInt(String s) {
        if (s.isEmpty()) {
            return 0;
        }

        int integer = roman.get(s.charAt(s.length() - 1));
        for (int i = s.length() - 2; i >= 0; i--) {
            int cur = roman.get(s.charAt(i));
            int last = roman.get(s.charAt(i + 1));
            integer = (cur >= last) ? (integer + cur) : (integer - cur); // error1: must >=, DCXXI=621 not 601
        }
        return integer;
    }

}

@@ -542,29 +542,697 @@
     
-public int romanToInt
+// My 2AC: much easier to understand now!%0A    public int romanToInt(String s) %7B%0A        Map%3CCharacter,Integer%3E map = new HashMap%3C%3E();%0A        map.put('I', 1);%0A        map.put('V', 5);%0A        map.put('X', 10);%0A        map.put('L', 50);%0A        map.put('C', 100);%0A        map.put('D', 500);%0A        map.put('M', 1000);%0A%0A        // Substract if less than prev, since IIV is impossible%0A        int prev = 0, num = 0;%0A        for (int i = s.length() - 1; i %3E= 0; i--) %7B%0A            int cur = map.get(s.charAt(i));%0A            num += (cur %3E= prev) ? cur : -cur;%0A            prev = cur;%0A        %7D%0A        return num;%0A    %7D%0A%0A    // My 1AC: bad dealing with cur and last%0A    public int romanToInt1
 (Str

leetcode-013 Roman to integer