LeetCode-12-Integer-to-Roman 无聊模拟题
来源:互联网 发布:耳机音量放大器软件 编辑:程序博客网 时间:2024/06/10 18:07
模拟阿拉伯数字转罗马数字,我tm就知道罗马数字12以下的怎么写,现去查了百度才知道原来罗马数字有7个字母,到了3999就不能再大了,不然还得用新字母,而且罗马数字直观来看并不能看出来是多少,MDZZ,也就能用在钟表里装个逼了。
全暴力模拟写的太麻烦,我比较懒,写了个函数优化了一下。
class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ #1000,500,100,50,10,5,1 #M,D,C,L,X,V,I #3999 is MMMCMXCIX ans="" if num/1000>0: for i in range(num/1000):ans+='M' if (num%1000)/100>0: ans+=self.arab2Roman((num%1000)/100,'C','D','M') if (num%100)/10>0: ans+=self.arab2Roman((num%100)/10,'X','L','C') if num%10>0: ans+=self.arab2Roman(num%10,'I','V','X') return ans def arab2Roman(self,num,I,V,X): """ int char char """ ans="" if num%10==1: ans=I elif num%10==2: ans=ans+I+I elif num%10==3: ans=ans+I+I+I elif num%10==4: ans=ans+I+V elif num%10==5: ans=ans+V elif num%10==6: ans=ans+V+I elif num%10==7: ans=ans+V+I+I elif num%10==8: ans=ans+V+I+I+I elif num%10==9: ans=ans+I+X else: ans=ans+X return ans
阅读全文
1 0
- LeetCode-12-Integer-to-Roman 无聊模拟题
- LeetCode-13-Roman-to-Integer 无聊模拟,递归
- leetcode第12题-Integer to Roman
- Leetcode 第12题Integer to Roman
- leetcode 第12题 Integer to Roman
- [LeetCode 12] Integer to Roman
- leetcode 12 Integer to Roman
- [leetcode 12] Integer to Roman
- [Leetcode] 12 - Integer to Roman
- LeetCode | #12 Integer to Roman
- leetcode.12----------Integer to Roman
- leetcode 12 Integer to Roman
- LeetCode 12 - Integer to Roman
- leetcode 12 -- Integer to Roman
- Leetcode #12-Integer to Roman
- LeetCode 12:"Integer to Roman"
- Leetcode#12 Integer to Roman
- leetcode-12Integer to Roman
- vs2010 无法将 复制到 超出了重试计数10
- CentOS6.8 详细安装步骤
- Redis、Memcache和MongoDB的区别
- Http请求中Content-Type
- SPSS Modeler中对字段指定的类型不充分的解决方法
- LeetCode-12-Integer-to-Roman 无聊模拟题
- python学习笔记-马哥2017
- 关于Java设计模式
- 微信小程序多行省略
- 富士康和运营商都坐不住了,这是LoRa物联网络要火的节奏
- 【IIS】IIS部署MVC5 网站 403.14 (1)
- 阿里笔试题-猴子摘桃子
- 「python」文件的读写
- 阿里quick_bi和biee报表工具的对比