A simple solution in Javascript


  • 1
    B
    var intToRoman = function(num) {
        if(num < 1 || num > 3999) return "";
        var numbers = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
        var symbols = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
        var ret = "";
        numbers.forEach(function(value, i) {
           if(num > 0) {
               if((~~(num/value)) > 0) {
                   var times = ~~(num / value);
                   num -= (times * value);
                   while(times > 0) {
                       ret += symbols[i];
                       times--;
                   }
               }
           } 
        });
        return ret;
    };

Log in to reply
 

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