Solution in Python 3


  • 0
    L

    class Solution(object):
    def intToRoman(self, num):
    Roman = ''
    Dic={3000:'MMM',2000:'MM',1000:'M'}
    Dic1={900:'CM',800:'DCCC',700:'DCC',600:'DC',500:'D',400:'CD',300:'CCC',200:'CC',100:'C',0:''}
    Dic2={90:'XC',80:'LXXX',70:'LXX',60:'LX',50:'L',40:'XL',30:'XXX',20:'XX',10:'X',0:''}
    Dic3={9:'IX',8:'VIII',7:'VII',6:'VI',5:'V',4:'IV',3:'III',2:'II',1:'I',0:''}
    str1=str(num)
    L=len(str1)

    Thousand

        if L == 4:
            t = int(str1[0])
            k = int(str1[1])
            l = int(str1[2])
            n = int(str1[3])
            Roman=Dic[1000*t]+Dic1[100*k]+Dic2[10*l]+Dic3[n]
        if L == 3:
            k = int(str1[0])
            l = int(str1[1])
            n = int(str1[2])
            Roman = Dic1[100*k]+Dic2[10*l]+Dic3[n]
        if L == 2:
            l = int(str1[0])
            n = int(str1[1])
            Roman = Dic2[10*l]+Dic3[n]
        if L == 1:
            n = int(str1[0])
            Roman = Dic3[n]
    
        return Roman

Log in to reply
 

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