码迷,mamicode.com
首页 > 其他好文 > 详细

整数转罗马数字

时间:2019-09-15 01:31:43      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:tor   res   str   原因   pytho   tco   字典   ble   ber   

思路: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)
        

整数转罗马数字

标签:tor   res   str   原因   pytho   tco   字典   ble   ber   

原文地址:https://www.cnblogs.com/dolisun/p/11520663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!