mycode  97.21%

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,'IV':4,'IX':9,'XL':40,'XC':90,'CD':400,'CM':900}
        res = 0
        flag = 0
        for i in range(len(s)-1):
            if flag:
                flag = 0
                continue
            temp = s[i] + s[i+1]
            if temp in dic:
                res += dic[temp]
                flag = 1
            else:
                res += dic[s[i]]
        if flag == 0:
            return res + dic[s[-1]]
        return res
                           

 

参考

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        roman_map = {
            "I": 1,
            "V": 5,
            "X": 10,
            "L": 50,
            "C": 100,
            "D": 500,
            "M": 1000
        }
        sum = 0
        for idx, c in enumerate(s[:-1]):
            num = roman_map[c]
            sum +=  (-num) if roman_map[s[idx+1]] > num else num#题目已经说了字母是按大到小,除了特殊的
        sum += roman_map[s[-1]]
        return sum