class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ table = {'M':1000, 'CM':900, 'D':500, 'CD':400, 'C':100, 'XC':90, 'L':50, 'XL': 40, 'X':10, 'IX':9, 'V':5, 'VI':4, 'I':1} result = 0 for i in range(len(s)): if i > 0 and table[s[i]] > table[s[i-1]]: result += table[s[i]] result -= 2 * table[s[i-1]] else: result += table[s[i]] return result