You are viewing a single comment's thread. Return to all comments →
it's almost the same as this one in leetcode: https://leetcode.com/problems/roman-to-integer/description/
but not using regex, enjoy:
class Solution(object): 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 s = s[::-1] last = None for i in s: if last and d[i]< last: sum -= d[i] * 2 sum += d[i] last = d[i] return sum
Seems like cookies are disabled on this browser, please enable them to open this website
Validating Roman Numerals
You are viewing a single comment's thread. Return to all comments →
it's almost the same as this one in leetcode: https://leetcode.com/problems/roman-to-integer/description/
but not using regex, enjoy: