解法一(112 ms 93.86%):
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
dic = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
res=0
for i in range(len(s)):
if i+1<len(s) and s[i]=='I' and (s[i+1]=='V' or s[i+1]=='X'):
res-=2
if i+1<len(s) and s[i]=='X' and (s[i+1]=='L' or s[i+1]=='C'):
res-=20
if i+1<len(s) and s[i]=='C' and (s[i+1]=='D' or s[i+1]=='M'):
res-=200
res+=dic[s[i]]
return res