代码拉取完成,页面将自动刷新
# I 1
# V 5
# X 10
# L 50
# C 100
# D 500
# M 1000
# I can be placed before V (5) and X (10) to make 4 and 9.
# X can be placed before L (50) and C (100) to make 40 and 90.
# C can be placed before D (500) and M (1000) to make 400 and 900.
#re.sub 神器!
import re
class Solution:
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
def func(matched):
dic={"V":"IX","L":"XC","D":"CM","I":"IV","X":"XL","C":"CD"}
s=matched.group()
return dic[s[0]]
s=[]
for k,v in zip([1000,500,100,50,10,5,1],"MDCLXVI") :
s.extend(v*(num//k))
num%=k
pattern=r"V?I{4}|L?X{4}|D?C{4}"
return re.sub(pattern,func,"".join(s))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。