Efficient javascript solution


  • 0
    S
    /**
     * @param {number} num
     * @return {string}
     */
    var intToRoman = function(num) {
      
        var Thousands = ["", "M", "MM", "MMM"];
        var Hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
        var Tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
        var Ones = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
        
       return Thousands[Math.floor(num/1000)]+Hundreds[Math.floor((num%1000)/100)]+Tens[Math.floor((num%100)/10)]+Ones[num%10];
    
    };
    

Log in to reply
 

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