O(n) simple to understand javascript solution with roman rules

  • 0

    from left to right, check for these 'special' rules first: IV(4), IX(9), XL(40), XC(90), CD(400), CM(900).
    if not found, add the current char's value to the solution and go to next char.

     * @param {string} s
     * @return {number}
    var romanToInt = function(s) {
      // 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 ans = 0;
      for(let i=0; i < s.length; i++){
          //check special rules that require lookahead
          switch (s[i]+s[i+1]) {
              case 'IV': ans+=4; i++; break;
              case 'IX': ans+=9; i++; break;
              case 'XL': ans+=40; i++; break;
              case 'XC': ans+=90; i++; break;
              case 'CD': ans+=400; i++; break;
              case 'CM': ans+=900; i++; break;
              // by default, switch over current ith char
              default: switch(s[i]){
                  case 'I': ans+=1; break;
                  case 'V': ans+=5; break;
                  case 'X': ans+=10; break;
                  case 'L': ans+=50; break;
                  case 'C': ans+=100; break;
                  case 'D': ans+=500; break;
                  case 'M': ans+=1000; break;
      return ans;

Log in to reply

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