Source of the topic:
https://leetcode.com/problems/roman-to-integer/
Test Instructions Analysis:
The title, contrary to the previous one, is the conversion of Roman numerals into Arabic numerals.
Topic Ideas:
Just know how the Roman numerals and Arabic numerals are converted. First make a dictionary of characters and values, {' I ': 1, ' V ': 5, ' X ': Ten, ' L ': +, ' C ': +, ' D ': +, ' M ': 1000}; If you find that the input string is smaller than the previous one, then there is 4,9, Then subtract the value of the previous character by two times.
Code (Python):
1 classsolution (object):2 defRomantoint (self, s):3 """4 : Type S:str5 : Rtype:int6 """7D = {'I': 1,'V'75A'X': 10,'L': 50,'C': 100,'D': 500,'M': 1000}8Ans =09Size =Len (s)Teni =0 One whileI <Size: A ifI > 0 andD[s[i]] > D[s[i-1]]: -Ans + = d[s[i]]-2 * d[s[i-1]] - Else: theAns + =D[s[i]] -i + = 1 - returnAns
View Code
Reprint Please specify source: http://www.cnblogs.com/chruny/p/4817835.html
[Leetcode] (python): 013-roman to Integer