Easy
, Msc
Question
将一个整数转化为罗马数字
Notes
假设数字范围1到 3999
Solution
罗马数字和数字的对应关系如下表
将相减的特殊情况都放到map中。
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
values = [1000,900,500,400,100,
90,50,40,10,9,5,4,1]
symbols = ['M','CM','D','CD',
'C','XC', 'L', 'XL',
'X', 'IX', 'V', 'IV', 'I']
i = 0
ret = ''
while num>0:
k = num/values[i]
for j in xrange(k):
ret += symbols[i]
num -= values[i]
i += 1
return ret