博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[leetcode]12. Integer to Roman
阅读量:5305 次
发布时间:2019-06-14

本文共 3112 字,大约阅读时间需要 10 分钟。

做好分类,注意边界。

Success
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

 

转载于:https://www.cnblogs.com/alfredsun/p/10868800.html

你可能感兴趣的文章
(转)Java中的String为什么是不可变的? -- String源码分析
查看>>
HNU 10362 A+B for Input-Output Practice (II)
查看>>
10.17动手动脑
查看>>
WPF中Image显示本地图片
查看>>
Windows Phone 7你不知道的8件事
查看>>
脚本删除文件下的文件
查看>>
实用拜占庭容错算法PBFT
查看>>
java b组 小计算器,简单计算器..
查看>>
java的二叉树树一层层输出,Java构造二叉树、树形结构先序遍历、中序遍历、后序遍历...
查看>>
php libevent 定时器,PHP 使用pcntl和libevent实现Timer功能
查看>>
php仿阿里巴巴,php实现的仿阿里巴巴实现同类产品翻页
查看>>
Node 中异常收集与监控
查看>>
七丶Python字典
查看>>
Excel-基本操作
查看>>
面对问题,如何去分析?(分析套路)
查看>>
Excel-逻辑函数
查看>>
面对问题,如何去分析?(日报问题)
查看>>
数据分析-业务知识
查看>>
nodejs vs python
查看>>
poj-1410 Intersection
查看>>