int to roman solution


  • 0
    G

    class Solution(object):
    def intToRoman(self, num):
    """
    :type num: int
    :rtype: str
    """
    roman = {}
    roman[1000] = "M"
    roman[900] = "CM"
    roman[500] = "D"
    roman[400] = "CD"
    roman[100] = "C"
    roman[90] = "XC"
    roman[50] = "L"
    roman[40] = "XL"
    roman[10] = "X"
    roman[9] = "IX"
    roman[5] = "V"
    roman[4] = "IV"
    roman[1] = "I"

        rom = ""
        while num > 0:
            for key,value in sorted(roman.iteritems(), reverse = True):
                while num>=key:
                    rom+=value
                    num=num-key
        return rom

Log in to reply
 

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