执行用时 : 112 ms, 在Roman to Integer的Python3提交中击败了100.00% 的用户
内存消耗 : 13.4 MB, 在Roman to Integer的Python3提交中击败了0.95% 的用户
class Solution:
def romanToInt(self, s: str) -> int:
dict1={"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
num=0
while(s!=""):
if s[0] in ["I", "X","C"]:
if len(s)!=1 and s[1] in ["V" , "L","D","X","C","M"]and dict1[s[1]]//dict1[s[0]] in [10,5]:
num=num+dict1[s[1]]-dict1[s[0]]
s=s[2:]
else:
num=num+dict1[s[0]]
s=s[1:]
else:
num=num+dict1[s[0]]
s=s[1:]
return num