Title: https://oj.leetcode.com/problems/roman-to-integer/
Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.
Ideas: See Code annotations
Code:
classSolution:#@return An integer defRomantoint (self, s):#from the post-trip scan, record the segmented number with a temporary variable. #if the current is larger than the previous one, the value of this paragraph should be the current value minus the previous value. such as IV = 5–1; #Otherwise, the current value is added to the result and the next record is started. such as VI = 5 + 1, ii=1+1 #time complexity O (n), Spatial complexity O (1)Dict = {'M': 1000,'D': 500,'C': 100,'L': 50,'X': 10,'V'75A'I': 1} res=0 forIinchRange (len (s)):ifI > 0 andDict[s[i]] > Dict[s[i-1]]: res+ = Dict[s[i]]-dict[s[i-1]] Else: Res+=Dict[s[i]]returnRes
[Leetcode] Roman to Integer @ Python