美文网首页
12. Integer to Roman

12. Integer to Roman

作者: chaowwwww | 来源:发表于2018-04-11 18:23 被阅读0次
class Solution:
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        s1 = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
        s2 = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
        s3 = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
        s4 = ["", "M", "MM", "MMM"]

        ans = []
        ans.append(s1[num % 10])
        num //= 10
        ans.append(s2[num % 10])
        num //= 10
        ans.append(s3[num % 10])
        num //= 10
        ans.append(s4[num % 10])
        ans.reverse()
        return ''.join(ans)

相关文章

网友评论

      本文标题:12. Integer to Roman

      本文链接:https://www.haomeiwen.com/subject/wzpzhftx.html