My Python Solution Most Reasonable!


  • 0
    F
    class Solution(object):
        def intToRoman(self, num):
            """
            :type num: int
            :rtype: str
            """
            s = ""
            d = {1: ['I', 'V'], 10: ['X', 'L'], 100: ['C', 'D'], 1000: ['M']}
            switcher = 1
            while num:
                tmp = num % 10
                num //= 10
                if tmp < 4:
                    tmps = tmp * d[switcher][0]
                elif tmp == 4:
                    tmps = d[switcher][0] + d[switcher][1]
                elif tmp < 9:
                    tmps = d[switcher][1] + (tmp - 5) * d[switcher][0]
                elif tmp == 9:
                    tmps = d[switcher][0] + d[switcher * 10][0]
                s = tmps + s
                switcher *= 10
            return s
    

Log in to reply
 

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