How to convert Roman numerals to integers by LeetCode
This article is about how LeetCode converts Roman numerals to integers. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
one
Topic description
In Roman numerals, the correspondence between letters and numbers is as follows:
I
one
V
five
X
ten
L
fifty
C
one hundred
D
five hundred
M
one thousand
Usually, large numbers are on the left and small numbers are on the right, such as VI means 6, when small numbers are on the left and large numbers are on the right, large numbers subtract decimals, such as IV means 4. The requirement for this topic is to give a Roman numeral to be converted to an integer.
two
Knowledge point
Idea: establish a hash table
Firstly, the hash correspondence table between Roman letters and numbers is established through the dictionary structure, and then the corresponding values are added or subtracted according to the size relationship between the two characters.
Class Solution: def romanToInt (self, s: str)-> int: RomanDict = {'Illustrated self, s: str)-> int: RomanDict = {' I]: if RomanDice [I]