Python Solution


  • 0
    1
    class Solution:
        def __init__(self):
            self.dict_s = {'I': 1, 'X': 10, 'C': 100, 'M': 1000,
                           'V': 5, 'L': 50, 'D': 500}
    
        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            rtype = 0
            for index, x in enumerate(s, index=1):
                num = self.dict_s.get(x)
                if index < len(s) and x != 'M' and num < self.dict_s.get(s[index]):
                    num = -num
                rtype += num
            return rtype
    

  • 0
    1

    @1ru said in Python Solution:

    for index, x in enumerate(s, index=1):

    i am sorry, this line should be:

    for index, x in enumerate(s, start=1):


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.