class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ l=len(s) r,i=0,0 roman={‘I‘:1,‘V‘:5,‘X‘:10,‘L‘:50,‘C‘:100,‘D‘:500,‘M‘:1000,‘CM‘:900,‘CD‘:400,‘XC‘:90,‘XL‘:40,‘IX‘:9,‘IV‘:4} while i<l: if i<l-1 and roman.has_key(s[i:i+2]): r+=roman[s[i:i+2]] i+=2 else: r+=roman[s[i]] i+=1 return r
时间: 2024-10-28 21:03:30