classSolution(object):defromanToInt(self,s):""" :type s: str :rtype: int """R2Imap={'M':1000,'D':500,'C':100,'L':50,'X':10,'V':5,'I':1}ret=0ifs=="":returnretindex=len(s)-2whileindex>=0:# CM, IV, IX ifR2Imap[s[index]]<R2Imap[s[index+1]]:ret-=R2Imap[s[index]]else:# M,D,C,L,X,V,Iret+=R2Imap[s[index]]index-=1# s[len(s)-1]ret+=R2Imap[s[-1]]returnret