代码:
class Solution(object):def romanToInt(self, s):""":type s: str:rtype: int"""dict1 = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}nums = 0t = len(s)i= 0while i<t :if s[i]=='I':if i+1 == t:nums+=dict1.get(s[i])i+=1else:if s[i+1] =='V':nums+=4i+=2elif s[i+1]=='X':nums+=9i+=2else:nums+=dict1.get(s[i])i+=1elif s[i]=='X':if i+1 == t:nums+=dict1.get(s[i])i+=1else:if s[i+1] =='L':nums+=40i+=2elif s[i+1]=='C':nums+=90i+=2else:nums+=dict1.get(s[i])i+=1elif s[i]=='C':if i+1 == t:nums+=dict1.get(s[i])i+=1else:if s[i+1] =='D':nums+=400i+=2elif s[i+1]=='M':nums+=900i+=2else:nums+=dict1.get(s[i])i+=1else:nums+=dict1.get(s[i])i+=1return nums