LeetCode--Integer to Roman
来源:互联网 发布:java开发项目案例 编辑:程序博客网 时间:2024/06/08 11:26
题目:
将数字num转为罗马数字格式
思路:
先给出所有的罗马数字表示组合,如dict所示dict_integer = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'}。再使用贪心算法,每次都从上列组合中选出可选择的最大数,将该表示加入输出字符串,再将num减去选中的最大数,直到num变为0结束。
代码(python):
class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ dict_integer = {1000:'M',900:'CM',500:'D',400:'CD',100:'C',90:'XC',50:'L',40:'XL',10:'X',9:'IX',5:'V',4:'IV',1:'I'} list_integer = [1000,900,500,400,100,90,50,40,10,9,5,4,1] output = '' temp = 0 while(num): if list_integer[temp]<=num: output = output+dict_integer[list_integer[temp]] num = num-list_integer[temp] else: temp=temp+1 continue return output
阅读全文
1 0
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Leetcode: Roman to Integer
- LeetCode Integer to Roman
- 【leetcode】Roman to Integer
- JSON的简单使用
- 005 逆阵求法方法二:矩阵初等变换法
- STL_不定长数组-vector
- Python网络爬虫(四):视频下载器
- iSCSI的配置笔记
- LeetCode--Integer to Roman
- 提升Android下内存的使用意识和排查能力
- 修仙 (KMP)
- 编程计算并输出两个整数的最大值
- ajax的同步请求
- 欢迎使用CSDN-markdown编辑器
- 5. OpenCV图像分割-watershed
- PyCharm使用问题
- java并发编程-创建线程