My Python solution


  • 0
    P

    My solution is to deal with the 9 and 4 ,because this two number is the line that make the notation change

    class Solution(object):
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            
            decimalisms = ('I','X','C','M')
            quinarys = ('V','L','D')
            
            pos = 0
            roman = ""
            while num > 0:
                mod = num%10
                num = num/10
                if mod == 9:
                    roman =decimalisms[pos] + decimalisms[pos+1]  + roman
                elif mod >= 4:
                    roman = decimalisms[pos]*(5-mod) + quinarys[pos] + decimalisms[pos]*(mod-5) + roman
                elif mod > 0:
                    roman = decimalisms[pos]*mod+roman
    
                pos += 1
                
            return roman
    

Log in to reply
 

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