12. Integer to Roman
直接greedy
从大到小搜索,这个的搜索速度其实完全取决于预输入的hash table
覆盖了多少,如果把II、III、XX、XXX这种也录进去的话就会变快...
class Solution:
def intToRoman(self, num: int) -> str:
t = {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 s, v in t.items():
res.append(num//s * v)
num %= s
return "".join(res)
Last updated
Was this helpful?