标签:while span code else elf int rom 内存 and
class Solution: def romanToInt(self, s: str) -> int: memo={‘I‘:1,‘IV‘:4,‘V‘:5,‘IX‘:9,‘X‘:10,‘XL‘:40,‘L‘:50,‘XC‘:90,‘C‘:100,‘CD‘:400,‘D‘:500,‘CM‘:900,‘M‘:1000} i=0 v=0 while i<len(s): if i<len(s)-1 and s[i] in [‘I‘,‘X‘,‘C‘]: if s[i:i+2] in [‘IV‘,‘IX‘,‘XL‘,‘XC‘,‘CD‘,‘CM‘]: v+=memo[s[i:i+2]] i+=2 else: v+=memo[s[i]] i+=1 else: v+=memo[s[i]] i+=1 return v
标签:while span code else elf int rom 内存 and
原文地址:https://www.cnblogs.com/taoyuxin/p/11693038.html