Java solution O(n), start from beginning


  • 1
    Z
    private Map<Character, Integer> map = new HashMap<Character, Integer>() {{
        put('I', 1);
        put('V', 5);
        put('X', 10);
        put('L', 50);
        put('C', 100);
        put('D', 500);
        put('M', 1000);
    }};
    
    public int romanToInt(String s) {
        if (s == null || s.isEmpty()) return 0;
        if (s.length() == 1) {
            return map.get(s.charAt(0));
        }
    
        int i = 0;
        int result = 0;
        while (i < s.length()) {
            Integer current = map.get(s.charAt(i));
            Integer forward = 0;
            int j = i + 1;
            if (j < s.length()) {
                forward = map.get(s.charAt(j));
            }
            if (current >= forward) {
                result += current;
                i++;
            } else {
                result += forward - current;
                i += 2;
            }
        }
        return result;
    }

Log in to reply
 

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