zoukankan      html  css  js  c++  java
  • [leetcode]12. Integer to Roman

    做好分类,注意边界。

    Success
    Details 
    Runtime: 52 ms, faster than 98.97% of Python3 online submissions forInteger to Roman.
    Memory Usage: 13.3 MB, less than 60.15% of Python3 online submissions for Integer to Roman.
     

    Submission Detail

    3999 / 3999 test cases passed.
    Status: 

    Accepted

    Runtime: 52 ms
    Memory Usage: 13.3 MB
    Submitted: 0 minutes ago
    class Solution:
        def intToRoman(self, num: int) -> str:
            if 1000 <= num <= 3999:
                units_digit = num % 10
                tens_digit = num % 100 - units_digit
                hundreds_digit = num%1000 - tens_digit - units_digit
                thousands_digit = num //1000
                units_ret = Solution().dealunitsdigit(units_digit)
                tens_ret = Solution().dealtensdigit(tens_digit)
                hundreds_ret = Solution().dealhundredsdigit(hundreds_digit)
                thousands_ret = ""
                for index in range(thousands_digit):
                    thousands_ret = thousands_ret + "M"
                return thousands_ret + hundreds_ret + tens_ret + units_ret
            elif 100 <= num < 1000:
                units_digit = num %10
                tens_digit = num%100 - units_digit
                hundreds_digit = num - tens_digit - units_digit
                units_ret = Solution().dealunitsdigit(units_digit)
                tens_ret = Solution().dealtensdigit(tens_digit)
                hundreds_ret = Solution().dealhundredsdigit(hundreds_digit)
                return hundreds_ret + tens_ret + units_ret
            elif 10 <= num < 100:
                units_digit = num %10
                tens_digit = num - units_digit
                units_ret = Solution().dealunitsdigit(units_digit)
                tens_ret = Solution().dealtensdigit(tens_digit)
                return tens_ret + units_ret
            elif 1 <= num < 10:
                ret = Solution().dealunitsdigit(num)
                return ret
        #1-9
        def dealunitsdigit(self,num:int) -> str:
            if num == 0:
                return ""
            if num == 4:
                return "IV"
            if num == 9:
                return "IX"
            if num <5:
                ret =""
                for index in range(num):
                    ret = ret + "I"
                return ret
            else:
                ret ="V"
                for index in range(num -5): #min V
                    ret = ret + "I"
                return ret
        #10,20,30,40...90
        def dealtensdigit(self,num:int) -> str:
            if num == 40:
                return "XL"
            if num == 90:
                return "XC"
            if num <50:
                ret =""
                for index in range(num//10):
                    ret = ret + "X"
                return ret
            else:
                ret ="L"
                for index in range((num -50)//10): #min L
                    ret = ret + "X"
                return ret
        #100,200,300...900
        def dealhundredsdigit(self,num:int) -> str:
            if num == 400:
                return "CD"
            if num == 900:
                return "CM"
            if num <500:
                ret =""
                for index in range(num//100):
                    ret = ret + "C"
                return ret
            else:
                ret ="D"
                for index in range((num -500)//100): #min L
                    ret = ret + "C"
                return ret
  • 相关阅读:
    多校第五场 归并排序+暴力矩阵乘+模拟+java大数&amp;记忆化递归
    Cocos2dx-Android 之Makefile通用高级写法
    出现异常 child-&gt;m_pParent == 0
    mybatis一对多关联查询——(九)
    mybatis一对一关联查询——(八)
    mybatis关联查询数据模型分析——(七)
    mybatis动态sql——(六)
    mybatis输入输出映射——(五)
    SqlMapConfig.xml全局配置文件介绍——(四)
    mybatis开发dao的方法——(三)
  • 原文地址:https://www.cnblogs.com/alfredsun/p/10868800.html
Copyright © 2011-2022 走看看