Javascript Solution


  • 0

    Leetcode Solution with Javascript

    var romanToInt = function(roman) {
      const ROMAN_TABLE = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 }
      let temp = 0
      let result = 0
      for (let elem of roman) {
        if (temp < ROMAN_TABLE[elem]) {
          result -= temp
          temp = ROMAN_TABLE[elem]
        } else if (temp > ROMAN_TABLE[elem]) {
          result += temp
          temp = ROMAN_TABLE[elem]
        } else {
          temp += ROMAN_TABLE[elem]
        }
      }
      return result + temp
    };
    

Log in to reply
 

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