My python solution, a little silly but pretty fast and easy to follow


  • 0
    G
    enter code hereclass Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        res=''
        res+='M'*(num//1000)
        
        num%=1000
        if num//100==9:
            res+='CM'
        if num//100<9 and num//100>=5:
            res+='D'+'C'*(num//100-5)
        if num//100==4:
            res+='CD'
        if num//100<4:
            res+='C'*(num//100)
        
        num%=100
        if num//10==9:
            res+='XC'
        if num//10<9 and num//10>=5:
            res+='L'+'X'*(num//10-5)
        if num//10==4:
            res+='XL'
        if num//10<4:
            res+='X'*(num//10)
        
        num%=10
        if num//1==9:
            res+='IX'
        if num//1<9 and num//1>=5:
            res+='V'+'I'*(num//1-5)
        if num//1==4:
            res+='IV'
        if num//1<4:
            res+='I'*(num//1)    
            
        return res

Log in to reply
 

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