|
| 1 | +<h2><a href="https://leetcode.com/problems/integer-to-roman">12. Integer to Roman</a></h2><h3>Medium</h3><hr><p>Seven different symbols represent Roman numerals with the following values:</p> |
| 2 | + |
| 3 | +<table> |
| 4 | + <thead> |
| 5 | + <tr> |
| 6 | + <th>Symbol</th> |
| 7 | + <th>Value</th> |
| 8 | + </tr> |
| 9 | + </thead> |
| 10 | + <tbody> |
| 11 | + <tr> |
| 12 | + <td>I</td> |
| 13 | + <td>1</td> |
| 14 | + </tr> |
| 15 | + <tr> |
| 16 | + <td>V</td> |
| 17 | + <td>5</td> |
| 18 | + </tr> |
| 19 | + <tr> |
| 20 | + <td>X</td> |
| 21 | + <td>10</td> |
| 22 | + </tr> |
| 23 | + <tr> |
| 24 | + <td>L</td> |
| 25 | + <td>50</td> |
| 26 | + </tr> |
| 27 | + <tr> |
| 28 | + <td>C</td> |
| 29 | + <td>100</td> |
| 30 | + </tr> |
| 31 | + <tr> |
| 32 | + <td>D</td> |
| 33 | + <td>500</td> |
| 34 | + </tr> |
| 35 | + <tr> |
| 36 | + <td>M</td> |
| 37 | + <td>1000</td> |
| 38 | + </tr> |
| 39 | + </tbody> |
| 40 | +</table> |
| 41 | + |
| 42 | +<p>Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> |
| 43 | + |
| 44 | +<ul> |
| 45 | + <li>If the value does not start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.</li> |
| 46 | + <li>If the value starts with 4 or 9 use the <strong>subtractive form</strong> representing one symbol subtracted from the following symbol, for example, 4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code> and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>. Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>), 40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> |
| 47 | + <li>Only powers of 10 (<code>I</code>, <code>X</code>, <code>C</code>, <code>M</code>) can be appended consecutively at most 3 times to represent multiples of 10. You cannot append 5 (<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol 4 times use the <strong>subtractive form</strong>.</li> |
| 48 | +</ul> |
| 49 | + |
| 50 | +<p>Given an integer, convert it to a Roman numeral.</p> |
| 51 | + |
| 52 | +<p> </p> |
| 53 | +<p><strong class="example">Example 1:</strong></p> |
| 54 | + |
| 55 | +<div class="example-block"> |
| 56 | +<p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> |
| 57 | + |
| 58 | +<p><strong>Output:</strong> <span class="example-io">"MMMDCCXLIX"</span></p> |
| 59 | + |
| 60 | +<p><strong>Explanation:</strong></p> |
| 61 | + |
| 62 | +<pre> |
| 63 | +3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) |
| 64 | + 700 = DCC as 500 (D) + 100 (C) + 100 (C) |
| 65 | + 40 = XL as 10 (X) less of 50 (L) |
| 66 | + 9 = IX as 1 (I) less of 10 (X) |
| 67 | +Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places |
| 68 | +</pre> |
| 69 | +</div> |
| 70 | + |
| 71 | +<p><strong class="example">Example 2:</strong></p> |
| 72 | + |
| 73 | +<div class="example-block"> |
| 74 | +<p><strong>Input:</strong> <span class="example-io">num = 58</span></p> |
| 75 | + |
| 76 | +<p><strong>Output:</strong> <span class="example-io">"LVIII"</span></p> |
| 77 | + |
| 78 | +<p><strong>Explanation:</strong></p> |
| 79 | + |
| 80 | +<pre> |
| 81 | +50 = L |
| 82 | + 8 = VIII |
| 83 | +</pre> |
| 84 | +</div> |
| 85 | + |
| 86 | +<p><strong class="example">Example 3:</strong></p> |
| 87 | + |
| 88 | +<div class="example-block"> |
| 89 | +<p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> |
| 90 | + |
| 91 | +<p><strong>Output:</strong> <span class="example-io">"MCMXCIV"</span></p> |
| 92 | + |
| 93 | +<p><strong>Explanation:</strong></p> |
| 94 | + |
| 95 | +<pre> |
| 96 | +1000 = M |
| 97 | + 900 = CM |
| 98 | + 90 = XC |
| 99 | + 4 = IV |
| 100 | +</pre> |
| 101 | +</div> |
| 102 | + |
| 103 | +<p> </p> |
| 104 | +<p><strong>Constraints:</strong></p> |
| 105 | + |
| 106 | +<ul> |
| 107 | + <li><code>1 <= num <= 3999</code></li> |
| 108 | +</ul> |
0 commit comments