整数转罗马数字

思路:https://leetcode-cn.com/problems/integer-to-roman/solution/tan-xin-suan-fa-by-liweiwei1419/

class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        numbers = [('M',1000), ('CM',900), ('D',500), ('CD',400), ('C',100), ('XC',90), ('L',50), ('XL',40), ('X',10), ('IX',9), ('V',5), ('IV',4), ('I',1)]
        res = []
        for s, n in numbers:
            if num == 0:
                break
            elif num >= n:
                res += [s]*(num//n)
                num %= n
        return ''.join(res)
        
posted @ 2019-09-14 22:50  Dolisun  阅读(142)  评论(0编辑  收藏  举报