Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to convert Roman numerals to integers by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:20:47 09月30日 Update

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]

Tags: Numbers Roman numerals integers content letters more knowledge articles titles hashes good practical two between size large numbers dictionaries characters decimals Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MariaDB OPPO Reno Huawei Linux