Simple code by Java HashMap:


  • 0
    X
    public int romanToInt(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        
        final Map<Character, Integer> m = new HashMap<>();
        m.put('I', 1);
        m.put('V', 5);
        m.put('X', 10);
        m.put('L', 50);
        m.put('C', 100);
        m.put('D', 500);
        m.put('M', 1000);
        
        int res = m.get(s.charAt(0));
        for (int i = 1, len = s.length(); i < len; i++) {
            int pre = m.get(s.charAt(i - 1));
            int cur = m.get(s.charAt(i));
            
            res += cur - (cur <= pre ? 0 : pre * 2);
        }
        
        return res;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.