Simple JS solution


  • 0
    /**
     * @param {number} num
     * @return {string}
     */
    var intToRoman = function(num) {
        var str = "";
        var weights = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
        var numerals= ["M",  "CM","D", "CD", "C","XC","L","XL", "X", "IX", "V", "IV", "I"];
        for(var i = 0; i < weights.length && num !== 0; i++) {
            var noOfNumeral = num / weights[i];
            for(var j = 1; j <= noOfNumeral; j++) {
                str += numerals[i];
            }
            num %= weights[i];
        }
        return str;
    };
    

Log in to reply
 

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