跳到主要内容

Integer to Roman

描述

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

分析

代码

# Integer to Roman
# 时间复杂度O(num),空间复杂度O(1)
class Solution:
def intToRoman(self, num: int) -> str:
radix = [1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1]
symbol = ["M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I"]

roman = []
i = 0
while num > 0:
count = num // radix[i]
num %= radix[i]
roman.extend([symbol[i]] * count)
i += 1
return "".join(roman)

相关题目