moonlightshadow123
6/19/2017 - 9:28 AM

13. Roman to Integer

  1. Roman to Integer

class Solution(object):
    def romanToInt(self, s):
        """
        :type s: str
        :rtype: int
        """
        valueDict = {'M':1000, 'D':500, 'C':100, 'L':50, 'X':10, 'V':5, 'I':1}
        total = 0
        preVal = float('inf')
        for eachChar in s:
            curVal = valueDict[eachChar]
            total += curVal if curVal <= preVal else curVal - 2*preVal
            preVal = curVal
        return total

https://leetcode.com/problems/roman-to-integer/#/description

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.