simple solution in javascript


  • 0
    S
    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function(s) {
        var roman = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000};
        var result = 0;
        for (var i=0; i<s.length;) {
            if(roman[s[i]] < roman[s[i+1]]) {
                result += (roman[s[i+1]] - roman[s[i]]);
                i=i+2;
            } else {
                result += roman[s[i]];
                i++;
            } 
            
        }
        return result;
    };
    

Log in to reply
 

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