解题思路
很简单直接上代码
https://leetcode-cn.com/problems/integer-to-roman/
代码
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
hm = {
1: 'I',
5: 'V',
10: 'X',
50: 'L',
100: 'C',
500: 'D',
1000: 'M',
}
mh = {
0: [],
1: [1],
2: [1, 1],
3: [1, 1, 1],
4: [1, 5],
5: [5],
6: [5, 1],
7: [5, 1, 1],
8: [5, 1, 1, 1],
9: [1, 10],
}
rtv = []
weight = 1000
while num:
h, num = divmod(num, weight)
for e in mh[h]:
rtv.append(hm[e*weight])
weight /= 10
return ''.join(rtv)