Solution by SonaBalsam


  • 0
    S
    class Solution(object):
        def romanToInt(self, s):
            """
            :type s: str
            :rtype: int
            """
            val_dict = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
            s = s.upper()
            total = 0
            while s:
                if len(s)== 1 or val_dict[s[0]] >= val_dict[s[1]]:
                    total += val_dict[s[0]]
                    s = s[1:]
                else:
                    total += val_dict[s[1]] - val_dict[s[0]]
                    s = s[2:]
            return total
    

Log in to reply
 

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