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

leetcode 12 -> Integer to Roman

时间:2019-03-10 21:06:28      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:return   man   def   bsp   ==   pre   str   style   solution   

 

class Solution(object):
    def intToRoman(self, num):
        """
        :type num: int
        :rtype: str
        """
        if num == 0:
            return ""
        if num >= 1000:
            return "M" + self.intToRoman(num - 1000)
        elif num >= 900:
            return "CM" + self.intToRoman(num - 900)
        elif num >= 500:
            return "D" + self.intToRoman(num - 500)
        elif num >= 400:
            return "CD" + self.intToRoman(num - 400)
        elif num >= 100:
            return "C" + self.intToRoman(num - 100)
        elif num >= 90:
            return "XC" + self.intToRoman(num - 90)
        elif num >= 50:
            return "L" + self.intToRoman(num - 50)
        elif num >= 40:
            return "XL" + self.intToRoman(num - 40)
        elif num >= 10:
            return "X" + self.intToRoman(num - 10)
        elif num >= 9:
            return "IX" + self.intToRoman(num - 9)
        elif num >= 5:
            return "V" + self.intToRoman(num - 5)
        elif num >= 4:
            return "IV" + self.intToRoman(num - 4)
        elif num >= 1:
            return "I" + self.intToRoman(num - 1)

 

leetcode 12 -> Integer to Roman

标签:return   man   def   bsp   ==   pre   str   style   solution   

原文地址:https://www.cnblogs.com/sea-stream/p/10506657.html

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