第4周 13. Roman to Integer[Easy]
Roman numerals are represented by seven different symbols: , , , , , and .
SymbolValueI 1V 5X 10L 50C 100D 500M 1000
For example, two is written as in Roman numeral, just two one's added together. Twelve is written as, , which is simply + . The number twenty seven is written as , which is + + .
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not . Instead, the number four is written as . Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as . There are six instances where subtraction is used:
can be placed before (5) and (10) to make 4 and 9.
can be placed before (50) and (100) to make 40 and 90.
can be placed before (500) and (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Input:"LVIII"Output:58Explanation:L = 50, V= 5, III = 3.