class Solution: def romanToInt(self, s: str) -> int: num_convert = {'I': 1, 'V': 5, 'X':10,'L':50,'C':100,'D':500,'M':1000} s_len=len(s) count=0 for i in range(s_len): if s[i] in num_convert: if i==s_len-1: count+=num_convert[s[i]] continue elif s[i]=='I' and s[i+1]=='V': count-=num_convert[s[i]] continue elif s[i]=='I' and s[i+1]=='X': count-=num_convert[s[i]] continue elif s[i]=='X' and s[i+1]=='L': count-=num_convert[s[i]] continue elif s[i]=='X' and s[i+1]=='C': count-=num_convert[s[i]] continue elif s[i]=='C' and s[i+1]=='D': count-=num_convert[s[i]] continue elif s[i]=='C' and s[i+1]=='M': count-=num_convert[s[i]] continue else: count+=num_convert[s[i]] return count s = "MCMXCIV" sol = Solution().romanToInt(s) print(sol)
标签:count,convert,continue,leedcode,整数,罗马数字,num,len,elif From: https://www.cnblogs.com/yyyjw/p/17936560.html