Python using digit places


  • 0
    E
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            ones, fives = {1:'I', 10:'X', 100:'C', 1000:'M'}, {1:'V', 10:'L', 100:'D'}
            res, place = [], 1
            while num:
                digit = num % 10
                if digit in (4,9):
                    if digit == 4: res.append(fives[place])
                    else: res.append(ones[place*10])
                    res.append(ones[place])
                else:
                    five = digit/5
                    one = digit - 5*five
                    for i in range(one):
                        res.append(ones[place])
                    for i in range(five):
                        res.append(fives[place])
                num /= 10; place *= 10
            return ''.join(res[::-1])
    

Log in to reply
 

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