|
| 1 | +<h2><a href="https://leetcode.com/problems/roman-to-integer">Roman to Integer</a></h2> <img src='https://img.shields.io/badge/Difficulty-Easy-brightgreen' alt='Difficulty: Easy' /><hr><p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> |
| 2 | + |
| 3 | +<pre> |
| 4 | +<strong>Symbol</strong> <strong>Value</strong> |
| 5 | +I 1 |
| 6 | +V 5 |
| 7 | +X 10 |
| 8 | +L 50 |
| 9 | +C 100 |
| 10 | +D 500 |
| 11 | +M 1000</pre> |
| 12 | + |
| 13 | +<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p> |
| 14 | + |
| 15 | +<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li> |
| 19 | + <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li> |
| 20 | + <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>Given a roman numeral, convert it to an integer.</p> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | +<p><strong class="example">Example 1:</strong></p> |
| 27 | + |
| 28 | +<pre> |
| 29 | +<strong>Input:</strong> s = "III" |
| 30 | +<strong>Output:</strong> 3 |
| 31 | +<strong>Explanation:</strong> III = 3. |
| 32 | +</pre> |
| 33 | + |
| 34 | +<p><strong class="example">Example 2:</strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input:</strong> s = "LVIII" |
| 38 | +<strong>Output:</strong> 58 |
| 39 | +<strong>Explanation:</strong> L = 50, V= 5, III = 3. |
| 40 | +</pre> |
| 41 | + |
| 42 | +<p><strong class="example">Example 3:</strong></p> |
| 43 | + |
| 44 | +<pre> |
| 45 | +<strong>Input:</strong> s = "MCMXCIV" |
| 46 | +<strong>Output:</strong> 1994 |
| 47 | +<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. |
| 48 | +</pre> |
| 49 | + |
| 50 | +<p> </p> |
| 51 | +<p><strong>Constraints:</strong></p> |
| 52 | + |
| 53 | +<ul> |
| 54 | + <li><code>1 <= s.length <= 15</code></li> |
| 55 | + <li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li> |
| 56 | + <li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> |
| 57 | +</ul> |
0 commit comments