# LeetCode 13. Roman to Integer

## Description

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

## Explanation

The problem is about writing a function to convert a Roman number to an Arabic number.

We need to have some basic knowledge about Roman numerals.

Roman numerals, as used today, are based on seven symbols:

 Symbol Value I V X L C D M 1 5 10 50 100 500 1,000

Numbers are formed by combining symbols and adding the values. Symbols are placed from left to right in order of value, starting with the largest. However, in a few specific cases, to avoid four characters being repeated in succession (such as IIII or XXXX), a subtractive notation is used: as in this table:

 Number Notation 4 9 40 90 400 900 IV IX XL XC CD CM
• I placed before V or X indicates one less, so four is IV (one less than five) and nine is IX (one less than ten)
• X placed before L or C indicates ten less, so forty is XL (ten less than fifty) and ninety is XC (ten less than a hundred)
• C placed before D or M indicates a hundred less, so four hundred is CD (a hundred less than five hundred) and nine hundred is CM (a hundred less than a thousand)

First we use a map to store the relationship between 7 basic Roman symbol and their values.

Then we read the input string from right to the left. Whenever we visit one roman symbol, we compare its values with right symbol value. If it is bigger than or equal to the right symbol value, we add the current symbol value to the result. Otherwise, we minus the current symbol value from the result.