Easier Approach For Integer To Roman Conversion In Py3


  • 0
    V
    class Solution:
        def intToRoman(self, num):
            RomanMap = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')]
            Roman = ''
            while num > 0:
                for i, r in RomanMap:
                    while num >= i:
                        Roman += r
                        num -= i
            return Roman 
    

Log in to reply
 

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