12. Integer to Roman
109
Mar 06, 2022
  1. Integer to Roman
class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        mapd = {'I':1, 'V':5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
        mapdd = {"IV": 4, "IX": 9, "XL":40, "XC": 90, "CD": 400,"CM": 900}
        d =    [1,   4,   5,  9,   10, 40,  50,  90,   100,400, 500,  900,   1000]
        dMap = ['I','IV','V','IX', 'X','XL','L', 'XC', 'C','CD', 'D', 'CM', 'M']
        
        n = num
        s = ""
        i = len(d) -1
        while n >= 0 and i>=0:
            if d[i]<= n and (n-d[i]) >=0:
                n -= d[i]
                s += dMap[i]
            else:
                i -= 1
        if n == 0:
            return s
Comments (0)