Javascript Solution


  • 0
    E
    var ROMAN = {
        M: 1000,
        D: 500,
        C: 100,
        L: 50,
        X: 10,
        V: 5,
        I: 1
    };
    
    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function(s) {
        var result = 0;
        var i = s.length - 1;
        while (i >= 0) {
            var roman = s[i];
            var num = ROMAN[roman];
            result += num;
            var j = i - 1;
            while(j >= 0 && ROMAN[s[j]] < num) {
                result -= ROMAN[s[j]]
                j -= 1;
            }
            i -= i - j;
        }
        return result;
    };
    

Log in to reply
 

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