题目链接:https://leetcode-cn.com/problems/roman-to-integer/
解题思路:
1 class Solution: 2 def romanToInt(self, s: str) -> int: 3 dict = {‘I‘:1,‘V‘:5, ‘X‘:10, ‘L‘:50, ‘C‘:100, ‘D‘:500, ‘M‘:1000} 4 n= len(s) 5 sum = 0 6 for i in range(n-1): 7 if dict[s[i]] < dict[s[i+1]]: 8 sum -= dict[s[i]] 9 else: 10 sum += dict[s[i]] 11 return sum + dict[s[-1]]
原文:https://www.cnblogs.com/vvzhang/p/14322752.html