class Solution:
def intToRoman(self, num: int) -> str:
# 贪心算法
# 使用哈希表,按照从大到小顺序排列
hashmap = {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'}
res = ''
for key in hashmap:
if num // key != 0:
count = num // key
res += hashmap[key] * count
num %= key
return res
整数转罗马数字
最新推荐文章于 2024-09-11 22:48:33 发布