Javascript Solution with only two lines logic


  • 0
    S
    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function(s) {
        var map = {
                I: 1,
                V: 5,
                X: 10,
                L: 50,
                C: 100,
                D: 500,
                M: 1000,
                IV: 4,
                IX: 9,
                XL: 40,
                XC: 90,
                CD: 400,
                CM: 900
            },
            sArray = s.split(''),
            res = 0;
    
        for (var i = 0; i < sArray.length; i++) {
            res += map[sArray[i] + sArray[i++ + 1]] || map[sArray[--i]];
        }
        return res;
    };
    

  • 0

    I like you use of a lookup table for the possible combinations not just the the single letters.


Log in to reply
 

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