python solution


  • 0
    F

    class Solution:

    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        d = {'I': 1, 'V' : 5, 'X' : 10, 'L' : 50, 'C' : 100, 'D' : 500, 'M' : 1000}
        sum = 0;
        pre = 0
        for i in s:
            
            if pre < d[i] :
                sum -= pre * 2
            sum += d[i];
                
            pre = d[i];
        return sum

Log in to reply
 

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