Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

roman to integer fastest way

function romanToInteger(s) {
    let 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
    }
    let result = 0;
    for (let i = 0; i < s.length; i++) {
        let current = s[i] + s[i + 1];
        if (map[current] !== undefined) {
            result += map[current];
            i++;
        } else {
            result += map[s[i]];
        }
    }
    return result;
}
 
PREVIOUS NEXT
Tagged: #roman #integer #fastest
ADD COMMENT
Topic
Name
7+7 =