id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string getSymbols(int letter, int count){\n string symbols = \"\";\n\n for(int i = 0; i < count; i++){\n switch(letter) {\n case 0:\n symbols += 'I';\n break;\n case 1:\n symbols += 'V';\n break;\n case 2:\n symbols += 'X';\n break;\n case 3:\n symbols += 'L';\n break;\n case 4:\n symbols += 'C';\n break;\n case 5:\n symbols += 'D';\n break;\n case 6: \n symbols += 'M';\n break;\n }\n }\n\n return symbols;\n }\n\n string getRepresentation(int num, int pow){\n string representation = \"\";\n switch(num) {\n case 4:\n representation = representation \n + getSymbols(pow * 2, 1)\n + getSymbols(pow * 2 + 1, 1);\n break;\n case 9:\n representation = representation\n + getSymbols(pow * 2, 1)\n + getSymbols(pow * 2 + 2, 1);\n break;\n default:\n if(num >= 5){\n representation += getSymbols(pow * 2 + 1, 1);\n num -= 5;\n }\n representation += getSymbols(pow *2, num);\n break;\n }\n return representation;\n }\n\n string intToRoman(int num) {\n string roman = \"\";\n for(int i = 3; i >= 0; i--){\n roman += getRepresentation(num / pow(10, i), i);\n num = num % (int)pow(10, i);\n }\n return roman;\n }\n};", "memory": "10945" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n \n string addNtimes(char ch, int n ){\n string str = \"\";\n for ( int i = 0; i < n; i++ ){\n str += ch;\n }\n return str;\n }\n \n string intToRoman(int num) {\n \n // map<int, char> getRomanFromDigit;\n // mp[1] = 'I';\n // mp[5] = 'V';\n // mp[10] = 'X';\n // mp[50] = 'L';\n // mp[100] = 'C';\n // mp[500] = 'D';\n // mp[1000] = 'M';\n string ans = \"\";\n int temp = num;\n int scale = pow(10, (to_string(num).length() - 1));\n cout << scale;\n \n while ( scale ){\n \n int currNum = (temp / scale) ;\n cout<<currNum << \" \" << scale<<endl;\n if ( scale*currNum >= 1000 ){\n ans += addNtimes('M', currNum);\n }\n else if ( scale*currNum >= 100 ){\n if ( scale*currNum < 400 ){\n ans += addNtimes('C', currNum);\n }\n else if ( scale*currNum == 400 ){\n ans += \"CD\";\n }\n else if ( scale*currNum < 900 ){\n ans += addNtimes('D', 1);\n ans += addNtimes('C', currNum-5);\n }\n else if ( scale*currNum == 900 ){\n ans += \"CM\";\n }\n }\n else if ( scale*currNum >= 10 ){\n if ( scale*currNum < 40 ){\n ans += addNtimes('X', currNum);\n } else if ( scale*currNum == 40 ){\n ans += \"XL\";\n }\n else if ( scale*currNum < 90 ){\n ans += addNtimes('L', 1);\n ans += addNtimes('X', currNum-5);\n }\n else if ( scale*currNum == 90 ){\n ans += \"XC\";\n }\n }\n else{\n if ( scale*currNum < 4 ){\n ans += addNtimes('I', currNum);\n }\n else if ( scale*currNum == 4 ){\n ans += \"IV\";\n }\n else if ( scale*currNum < 9 ){\n ans += addNtimes('V', 1);\n ans += addNtimes('I', currNum-5);\n }\n else if ( scale*currNum == 9 ){\n ans += \"IX\";\n }\n }\n \n temp %= scale;\n scale /= 10;\n }\n return ans;\n \n \n \n }\n};", "memory": "10945" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\nstring intToRoman(int num) {\n int thousands = num / 1000;\n num = num - 1000 * thousands;\n int fiveHundreds = num / 500;\n num = num - 500 * fiveHundreds;\n int hundreds = num / 100;\n num = num - 100 * hundreds;\n int fifties = num / 50;\n num = num - 50 * fifties;\n int tens = num / 10;\n num = num - 10 * tens;\n int fives = num / 5;\n num = num - 5 * fives;\n string romanian = \"\";\n while (thousands > 0) {\n romanian.append(\"M\");\n thousands--;\n }\n checkFormat(romanian, hundreds, fiveHundreds, \"C\", \"D\", \"M\");\n checkFormat(romanian, tens, fifties, \"X\", \"L\", \"C\");\n checkFormat(romanian, num, fives, \"I\", \"V\", \"X\");\n\n return romanian;\n}\n\nvoid checkFormat(string &romanian, int first, int second, string firstChar, string secondChar, string thirdChar) {\n if (first == 4) {\n if (second == 1)romanian.append(firstChar + thirdChar);\n else romanian.append(firstChar + secondChar);\n } else {\n if (second == 1) romanian.append(secondChar);\n while (first > 0) {\n romanian.append(firstChar);\n first--;\n }\n }\n}\n};", "memory": "11071" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string conv(int n) {\n if (n>=1000) {\n switch(n){\n case 1000:\n return \"M\";\n break;\n case 2000:\n return \"MM\";\n break;\n case 3000:\n return \"MMM\";\n break;\n }\n }\n\n else if(n<1000&&n>=100){\n switch(n){\n case 100:\n return \"C\";\n break;\n case 200:\n return \"CC\";\n break;\n case 300:\n return \"CCC\";\n break;\n case 400:\n return \"CD\";\n break;\n case 500:\n return \"D\";\n break;\n case 600:\n return \"DC\";\n break;\n case 700:\n return \"DCC\";\n break;\n case 800:\n return \"DCCC\";\n break;\n case 900:\n return \"CM\";\n break;\n }\n }\n\n else if(n<100&&n>=10){\n switch(n){\n case 10:\n return \"X\";\n break;\n case 20:\n return \"XX\";\n break;\n case 30:\n return \"XXX\";\n break;\n case 40:\n return \"XL\";\n break;\n case 50:\n return \"L\";\n break;\n case 60:\n return \"LX\";\n break;\n case 70:\n return \"LXX\";\n break;\n case 80:\n return \"LXXX\";\n break;\n case 90:\n return \"XC\";\n break;\n }\n }\n\n else{\n switch(n){\n case 1:\n return \"I\";\n break;\n case 2:\n return \"II\";\n break;\n case 3:\n return \"III\";\n break;\n case 4:\n return \"IV\";\n break;\n case 5:\n return \"V\";\n break;\n case 6:\n return \"VI\";\n break;\n case 7:\n return \"VII\";\n break;\n case 8:\n return \"VIII\";\n break;\n case 9:\n return \"IX\";\n break;\n }\n }\n return \"\";\n }\n\n string intToRoman(int num) {\n string ans;\n int u = 0, t = 0, h = 0, th = 0;\n if (num >= 1000) {\n u = num % 10; \n num /= 10;\n t = num % 10; \n num /= 10;\n h = num % 10;\n num /= 10;\n ans += conv(num * 1000);\n ans += conv(h * 100);\n ans += conv(t * 10);\n ans += conv(u); \n }\n\n else if (num < 1000 && num >= 100) {\n u = num % 10;\n num /= 10;\n t = num % 10; \n num /= 10;\n ans += conv(num * 100);\n ans += conv(t * 10);\n ans += conv(u); \n } else if (num < 100 && num >= 10) {\n u = num % 10; \n num /= 10;\n ans += conv(num * 10);\n ans += conv(u);\n } else {\n ans += conv(num);\n }\n return ans;\n }\n};", "memory": "11071" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n return convertRoman(num/1000,4) + convertRoman((num%1000)/100,3) + convertRoman((num%100)/10,2) + convertRoman(num%10,1); \n }\n string convertRoman(int digit, int placeValue) {\n string res= \"\";\n switch(placeValue) {\n case 4:\n res = repeat(\"M\", digit);\n break;\n case 3:\n if(digit>=1 && digit<4) res+=repeat(\"C\",digit);\n else if(digit==4) res+=\"CD\";\n else if(digit==5) res+=\"D\";\n else if(digit>5 && digit<9) res+=\"D\"+repeat(\"C\",(digit-5));\n else if(digit==9) res+=\"CM\";\n break;\n case 2:\n if(digit>=1 && digit<4) res+=repeat(\"X\",digit);\n else if(digit==4) res+=\"XL\";\n else if(digit==5) res+=\"L\";\n else if(digit>5 && digit<9) res+=\"L\"+repeat(\"X\",(digit-5));\n else if(digit==9) res+=\"XC\";\n break;\n case 1:\n if(digit>=1 && digit<4) res+=repeat(\"I\", digit);\n else if(digit==4) res+=\"IV\";\n else if(digit==5) res+=\"V\";\n else if(digit>5 && digit<9) res+=\"V\"+repeat(\"I\",(digit-5));\n else if(digit==9) res+=\"IX\";\n break;\n }\n return res;\n }\n string repeat(const string& input, unsigned num) {\n string ret;\n ret.reserve(input.size() * num);\n while (num--) ret += input;\n return ret;\n }\n};", "memory": "11198" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n std::string thousandsToRoman(int thousands) {\n return std::string(thousands, 'M');\n }\n\n std::string toRoman(int value, std::string currentDigit, std::string halfWayDigit, std::string nextDigit) {\n if (value == 9) {\n return currentDigit + nextDigit;\n } else if (value > 5) {\n value -= 5;\n return halfWayDigit + toRoman(value, currentDigit, halfWayDigit, nextDigit);\n } else if (value == 5) {\n return halfWayDigit;\n } else if (value == 4) {\n return currentDigit + halfWayDigit;\n } else {\n return std::string(value, currentDigit[0]);\n }\n }\n\n\n std::string intToRoman(int num) {\n auto thousands = thousandsToRoman(num / 1000);\n num = num % 1000;\n auto hundreds = toRoman(num / 100, \"C\", \"D\", \"M\");\n num = num % 100;\n auto tens = toRoman(num / 10, \"X\", \"L\", \"C\");\n num = num % 10;\n auto ones = toRoman(num, \"I\", \"V\", \"X\");\n return thousands + hundreds + tens + ones;\n }\n};", "memory": "11198" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string appendValue(string lower, string upper, string upperUpper, string sol, int digit) {\n if (digit < 4) {\n for (size_t i = 0; i < digit; i++) {\n sol = lower + sol;\n }\n }\n else if (digit == 4) {\n sol = lower + upper + sol;\n }\n else if (digit < 9){\n for (size_t i = 0; i < digit - 5; i++) {\n sol = lower + sol;\n }\n sol = upper + sol;\n }\n else {\n sol = lower + upperUpper + sol;\n }\n return sol;\n }\n\n string intToRoman(int num) {\n string sol = \"\";\n int currentPlace = 1;\n\n while (num > 0) {\n int temp = (num / 10) * 10;\n int digit = num - temp;\n if (currentPlace == 1) {\n sol = appendValue(\"I\", \"V\", \"X\", sol, digit);\n }\n else if (currentPlace == 10) {\n sol = appendValue(\"X\", \"L\", \"C\", sol, digit); \n }\n else if (currentPlace == 100) {\n sol = appendValue(\"C\", \"D\", \"M\", sol, digit);\n }\n else if (currentPlace == 1000) {\n for (size_t i = 0; i < digit; i++) {\n sol = \"M\" + sol;\n }\n }\n\n num /= 10;\n currentPlace *= 10;\n\n }\n return sol;\n }\n};", "memory": "11324" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n\n string temp = to_string(num);\n\n int len = temp.length();\n\n string ans=\"\";\n\n while(num)\n {\n if(len==4)\n {\n ans+=\"M\";\n num-=1000;\n }\n\n else if(len==3)\n {\n if(temp[0]=='9')\n {\n ans+=\"CM\";\n num-=900;\n }\n\n else if(temp[0]=='4')\n {\n ans+=\"CD\";\n num-=400;\n }\n\n else if(num>=500)\n {\n ans+=\"D\";\n num-=500;\n }\n\n else\n {\n ans+=\"C\";\n num-=100;\n }\n }\n\n else if(len==2)\n {\n if(temp[0]=='9')\n {\n ans+=\"XC\";\n num-=90;\n }\n\n else if(temp[0]=='4')\n {\n ans+=\"XL\";\n num-=40;\n }\n\n else if(num>=50)\n {\n ans+=\"L\";\n num-=50;\n }\n\n else\n {\n ans+=\"X\";\n num-=10;\n }\n }\n\n else\n {\n if(temp[0]=='9')\n {\n ans+=\"IX\";\n num-=9;\n }\n\n else if(temp[0]=='4')\n {\n ans+=\"IV\";\n num-=4;\n }\n\n else if(num>=5)\n {\n ans+=\"V\";\n num-=5;\n }\n\n else\n {\n ans+=\"I\";\n num-=1;\n }\n }\n\n temp = to_string(num);\n len = temp.length();\n }\n\n return ans;\n \n }\n};", "memory": "11450" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "#include <math.h>\nclass Solution {\npublic:\n string intToRoman(int num) {\n string ans = \"\";\n int n = (to_string(num).length());\n for (int i = n-1; i >= 0; --i) {\n int x = pow(10, i);\n int m = num / x;\n if (x == 1000) {\n ans += string(m, 'M');\n }\n else if (x == 100) {\n if (m < 4) ans += string(m, 'C');\n else if (m == 4) ans += \"CD\";\n else if (m < 9) ans += 'D' + string((m - 5), 'C');\n else ans += \"CM\";\n }\n else if (x == 10) {\n if (m < 4) ans += string(m, 'X');\n else if (m == 4) ans += \"XL\";\n else if (m >= 5 && m < 9) ans += 'L' + string((m - 5), 'X');\n else ans += \"XC\";\n }\n else {\n if (m < 4) ans += string(m, 'I');\n else if (m == 4) ans += \"IV\";\n else if (m < 9) ans += 'V' + string((m - 5), 'I');\n else ans += \"IX\";\n }\n num = num % x;\n }\n return ans;\n }\n};", "memory": "11450" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string roman(int num){\n if(num==1)return \"I\";\n else if(num==2)return \"II\";\n else if(num==3)return \"III\";\n else if(num==4)return \"IV\";\n else if(num==5)return \"V\";\n else if(num==6)return \"VI\";\n else if(num==7)return \"VII\";\n else if(num==8)return \"VIII\";\n else if(num==9)return \"IX\";\n else if(num==10)return \"X\";\n else if(num==20)return \"XX\";\n else if(num==30)return \"XXX\";\n else if(num==40)return \"XL\";\n else if(num==50)return \"L\";\n else if(num==60)return \"LX\";\n else if(num==70)return \"LXX\";\n else if(num==80)return \"LXXX\";\n else if(num==90)return \"XC\";\n else if(num==100)return \"C\";\n else if(num==200)return \"CC\";\n else if(num==300)return \"CCC\";\n else if(num==400)return \"CD\";\n else if(num==500)return \"D\";\n else if(num==600)return \"DC\";\n else if(num==700)return \"DCC\";\n else if(num==800)return \"DCCC\";\n else if(num==900)return \"CM\";\n else if(num==1000)return \"M\";\n else if(num==2000)return \"MM\";\n else if(num==3000)return \"MMM\";\n else return \"\";\n }\n string intToRoman(int num) {\n int n = to_string(num).length();\n int placeValue = pow(10, n-1);\n string Roman=\"\";\n while(num!=0){\n int digit = num/placeValue;\n Roman = Roman + roman(digit*placeValue);\n num%=placeValue;\n placeValue/=10;\n\n }\n\n return Roman;\n }\n};", "memory": "11576" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\nstring intToRoman(int num) {\n vector<pair<int, string>> mapping = {\n {1000, \"M\"}, {500, \"D\"}, {100, \"C\"}, \n {50, \"L\"}, {10, \"X\"}, {5, \"V\"}, {1, \"I\"} \n };\n string roman = \"\";\n // i = 0. 2 4. 6\n // only work with 1000, 100, 10, 1\n for (int i = 0; i < mapping.size(); i += 2) {\n int div = num / mapping[i].first;\n if (div == 9) {\n // 910 / 100 = 9 --> [100][1000]\n roman += mapping[i].second + mapping[i-2].second;\n num -= 9 * mapping[i].first;\n } else if (div == 4) {\n // 410 / 100 = 4 --> [100][500]\n roman += mapping[i].second + mapping[i-1].second;\n num -= 4 * mapping[i].first;\n } else {\n if (div >= 5) {\n roman += mapping[i-1].second;\n num -= mapping[i-1].first;\n div -= 5;\n }\n // For case 3->1, 30->10 etc\n while (div-- > 0) {\n roman += mapping[i].second;\n num -= mapping[i].first;\n }\n }\n }\n return roman;\n}\n};", "memory": "11703" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string get_char(int digit, int len) {\n if (digit <= 3) {\n switch (len) {\n case 1:\n return string(digit, 'I');\n case 2:\n return string(digit, 'X');\n case 3:\n return string(digit, 'C');\n case 4:\n return string(digit, 'M');\n default:\n return \"\";\n }\n }\n if (digit == 4) {\n switch (len) {\n case 1:\n return \"IV\";\n case 2:\n return \"XL\";\n case 3:\n return \"CD\";\n default:\n return \"\";\n }\n }\n if (digit == 5) {\n switch (len) {\n case 1:\n return \"V\";\n case 2:\n return \"L\";\n case 3:\n return \"D\";\n default:\n return \"\";\n }\n }\n if (digit > 5 && digit < 9) {\n switch (len) {\n case 1:\n return \"V\" + string(digit - 5, 'I');\n case 2:\n return \"L\" + string(digit - 5, 'X');\n case 3:\n return \"D\" + string(digit - 5, 'C');\n default:\n return \"\";\n }\n }\n if (digit == 9) {\n switch (len) {\n case 1:\n return \"IX\";\n case 2:\n return \"XC\";\n case 3:\n return \"CM\";\n default:\n return \"\";\n }\n }\n return \"\";\n }\n string intToRoman(int num) {\n vector<int> digits;\n int tmp = num;\n while (tmp > 0) {\n digits.push_back(tmp % 10);\n tmp = tmp / 10;\n }\n std::reverse(digits.begin(), digits.end());\n\n int length = digits.size();\n string result = \"\";\n int i = 0;\n while (i < length) {\n result += get_char(digits[i], length - i);\n ++i;\n }\n return result;\n }\n};", "memory": "11829" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> roman = {\"I\", \"V\", \"X\", \"L\", \"C\", \"D\", \"M\"};\n vector<int> values = {1, 5, 10, 50, 100, 500, 1000};\n string result;\n int i = 6;\n while (num > 0) {\n int div = num / values[i];\n num %= values[i];\n if (div == 0) {\n i -= 2;\n continue;\n }\n if (div == 9) {\n result += roman[i] + roman[i + 2];\n } else if (div == 4) {\n result += roman[i] + roman[i + 1];\n } else {\n if (div >= 5) {\n result += roman[i + 1];\n div -= 5;\n }\n while (div--) {\n result += roman[i];\n }\n }\n i -= 2;\n }\n return result;\n }\n};", "memory": "11955" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
1
{ "code": "class Solution {\npublic:\n vector<string> c1 {\"IV\", \"XL\", \"CD\"};\n vector<string> c2 {\"IX\", \"XC\", \"CM\"};\n char c3[4] = {'I', 'X', 'C', 'M'};\n char c4[3] = {'V', 'L', 'D'};\n string intToRoman(int num) {\n string ret, str = to_string(num);\n while(str.size() > 0) {\n if(str[0]=='4') { ret += c1[str.size()-1]; str.erase(0,1); continue; }\n if(str[0]=='9') { ret += c2[str.size()-1]; str.erase(0,1); continue; }\n if(str[0]-'0' < 5) { ret.append(str[0]-'0', c3[str.size()-1]); str.erase(0,1); continue; }\n if(str[0]-'0' >= 5) { ret += c4[str.size()-1]; str[0] = '0' + (str[0]-'5'); continue; }\n } return ret;\n }\n};", "memory": "11955" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n int n=1;\n if(num>999)\n n=1000;\n else if(num>99)\n n=100;\n else if(num>9)\n n=10;\n unordered_map<int,char> number_to_char;\n number_to_char[1]='I';\n number_to_char[5]='V';\n number_to_char[10]='X';\n number_to_char[50]='L';\n number_to_char[100]='C';\n number_to_char[500]='D';\n number_to_char[1000]='M';\n\n string answer;\n while(n>0)\n {\n int number=num%(n*10)/n;\n if(number<=3)\n for(int i=0;i<number;i++)\n answer.push_back(number_to_char[1*n]);\n else if(number==4)\n {\n answer.push_back(number_to_char[1*n]);\n answer.push_back(number_to_char[5*n]);\n }\n else if(number==5)\n answer.push_back(number_to_char[5*n]);\n else if(number<=8)\n {\n answer.push_back(number_to_char[5*n]);\n for(int i=5;i<number;i++)\n answer.push_back(number_to_char[1*n]);\n }\n else \n {\n answer.push_back(number_to_char[1*n]);\n answer.push_back(number_to_char[10*n]);\n }\n n/=10;\n \n }\n return answer;\n\n \n\n }\n};", "memory": "12081" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n unordered_map<int, char> index{\n {1, 'I'},\n {5, 'V'},\n {10, 'X'},\n {50, 'L'},\n {100, 'C'},\n {500, 'D'},\n {1000, 'M'}\n };\n int div = 1000;\n string s;\n while (div > 0) {\n int x = num / div;\n num = num % div;\n while (x > 0) {\n if (x == 4 || x == 9) {\n s.push_back(index[div]);\n x++;\n } else if (x == 10) {\n s.push_back(index[div * 10]);\n x = 0;\n } else if (x >= 5) {\n s.push_back(index[div * 5]);\n x -= 5;\n } else {\n s.push_back(index[div]);\n x--;\n }\n }\n div = div / 10;\n }\n return s;\n }\n};", "memory": "12208" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n// unordered_map <char,int> v{\n // {'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};\n // int ans=0;\n // for(int i=0;i<s.size();i++){\n // int m=v[s[i]];\n // if(s[i]=='I'&&s[i+1]&&s[i+1]=='V'){\n // m=4;\n // i++;\n // }\n // if(s[i]=='I'&&s[i+1]&&s[i+1]=='X'){\n // m=9;\n // i++;\n // }\n // if(s[i]=='X'&&s[i+1]&&s[i+1]=='L'){\n // m=40;\n // i++;\n // }\n // if(s[i]=='X'&&s[i+1]&&s[i+1]=='C'){\n // m=90;\n // i++;\n // }\n // if(s[i]=='C'&&s[i+1]&&s[i+1]=='D'){\n // m=400;\n // i++;\n // }\n // if(s[i]=='C'&&s[i+1]&&s[i+1]=='M'){\n // m=900;\n // i++;\n // }\n // ans+=m;\n // }\n // return ans;\n \n string intToRoman(int num) {\n string ans=\"\";\n unordered_map <char,int> mp{\n {1,'I'},{5,'V'},{10,'X'},{50,'L'},{100,'C'},{500,'D'},{1000,'M'}\n };\n\n int count=0;\n if(num/1000){\n count=num/1000;\n while(count){\n ans+='M';\n num-=1000;\n count--;\n }\n }\n if(num/100){\n while(num/100){\n if(num/100==9){\n ans+=\"CM\";\n num-=900;\n }\n if(num/100>=5){\n ans+='D';\n num-=500;\n }\n if(num/100==4){\n ans+=\"CD\";\n num-=400;\n }\n if(num/100){\n count=num/100;\n while(count){\n ans+='C';\n num-=100;\n count--;\n }\n }\n }\n }\n\n if(num/10){\n while(num/10){\n if(num/10==9){\n ans+=\"XC\";\n num-=90;\n }\n if(num/10>=5){\n ans+='L';\n num-=50;\n }\n if(num/10==4){\n ans+=\"XL\";\n num-=40;\n }\n if(num/10){\n count=num/10;\n while(count){\n ans+='X';\n num-=10;\n count--;\n }\n }\n }\n }\n if(num){\n while(num){\n if(num==9){\n ans+=\"IX\";\n num-=9;\n }\n if(num>=5){\n ans+='V';\n num-=5;\n }\n if(num==4){\n ans+=\"IV\";\n num-=4;\n }\n if(num){\n while(num){\n ans+='I';\n num--;\n }\n }\n }\n }\n return ans;\n }\n};", "memory": "12334" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n int n[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n vector<string> s {\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n int i =0;\n string str=\"\";\n while (num>0){\n if (num>=n[i]){\n str=str+s[i];\n num-=n[i];\n } else{\n i++;\n }\n }\n return str;\n }\n};", "memory": "12460" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "/**\n 10, 5, 1\n0 - 0, 0, 0\n1 - 0, 0, 1\n2 - 0, 0, 2\n3 - 0, 0, 3\n4 - 0, 1, 1\n5 - 0, 1, 0\n6 - 0, 1, 1\n7 - 0, 1, 2\n8 - 0, 1, 3\n9 - 1, 0, 1\n*/\n\nclass Solution {\npublic:\n string intToRoman(int num) {\n string ret;\n unordered_map<int, char> digit_to_roman = \n {\n {1, 'I'},\n {5, 'V'},\n {10, 'X'},\n {50, 'L'},\n {100, 'C'},\n {500, 'D'},\n {1000, 'M'},\n };\n\n for (const auto& decimal : {1000, 100, 10, 1})\n {\n int digit = num / decimal;\n num -= digit * decimal;\n if(digit == 4)\n {\n ret.push_back(digit_to_roman[decimal]);\n ret.push_back(digit_to_roman[5*decimal]);\n }\n else if(digit == 9)\n {\n ret.push_back(digit_to_roman[decimal]);\n ret.push_back(digit_to_roman[10*decimal]);\n }\n else\n {\n if (digit >=5)\n {\n ret.push_back(digit_to_roman[5*decimal]);\n digit -= 5;\n }\n for (;digit > 0; digit--)\n {\n ret.push_back(digit_to_roman[decimal]);\n }\n }\n }\n return ret; \n }\n};", "memory": "12460" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> cs = {\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n vector<int> vs = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n string ans;\n for (int i = 0; i < cs.size(); ++i) {\n while (num >= vs[i]) {\n num -= vs[i];\n ans += cs[i];\n }\n }\n return ans;\n }\n};", "memory": "12586" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n int times;\n string symbol = \"\";\n vector<int>nums = {1,4,5,9,10,40,50,90,100,400,500,900,1000};\n vector<string>roman = {\"I\",\"IV\",\"V\",\"IX\",\"X\",\"XL\",\"L\",\"XC\",\"C\",\"CD\",\"D\",\"CM\",\"M\"};\n for(int i=nums.size()-1;i>=0;i--){\n times = num/nums[i];\n for(int j=0;j<times;j++){\n symbol += roman[i];\n }\n num = num%nums[i];\n }\n return symbol;\n }\n};", "memory": "12713" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution \n{\npublic:\n string intToRoman(int num) \n {\n vector<string> roman ={ \"M\",\"CM\",\"D\",\"CD\",\"C\",\"XC\",\"L\",\"XL\",\"X\",\"IX\",\"V\",\"IV\",\"I\"};\n vector<int> value ={1000,900,500,400,100,90,50,40,10,9,5,4,1};\n string ans = \"\";\n int temp=num,i;\n for(i=1;i<=num/1000;i++)\n ans+=roman[0];\n num=num%1000;\n for(i=1;i<value.size();i++)\n {\n while(num>=value[i])\n {\n ans+=roman[i];\n num-=value[i];\n }\n }\n return ans; \n }\n};", "memory": "12713" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int>val{1000, 900, 500, 400, 100, 90, 50,\n 40, 10, 9, 5, 4, 1};\n vector<string>sym{\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\",\n \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n\n string intToRoman(int num) {\n\n string result = \"\";\n\n\n for (int i = 0; i < 13; i++) {\n if (num == 0){\n break;\n }\n int timer = num / val[i];\n\n while (timer--) {\n result += sym[i];\n }\n num = num % val[i];\n }\n return result;\n }\n};", "memory": "12839" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> cs = {\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n vector<int> vs = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n string ans;\n for (int i = 0; i < cs.size(); ++i) {\n while (num >= vs[i]) {\n num -= vs[i];\n ans += cs[i];\n }\n }\n return ans;\n }\n};", "memory": "12839" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> val{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n vector<string> sym{\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n \n string intToRoman(int num) {\n \n string result = \"\";\n int i = 0;\n while(num > 0) {\n while(num >= val[i]) {\n num -= val[i];\n result += sym[i];\n }\n i++;\n }\n return result;\n \n }\n};", "memory": "12965" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n \nvector<int> values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\nvector<string> symbols = {\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\",\"X\", \"IX\", \"V\", \"IV\", \"I\"};\n\n string ans=\"\";\n for(int i=0;i<values.size();i++){\n while(num>=values[i]){\n ans+=symbols[i];\n num-=values[i];\n }\n }\nreturn ans;\n \n }\n};", "memory": "12965" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (num)\n {\n if (num/it->first)\n {\n if (num/it->first==9)\n {\n ittemp = it;\n --ittemp;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*9;\n ++it;\n ++it;\n }\n else if (num/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*4;\n ++it;\n ++it;\n }\n else if (num/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n num-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n num-=it->first;\n }\n }\n else \n {\n ++it;\n ++it;\n }\n }\n string out = *ans;\n delete ans;\n return out;\n }\n};", "memory": "13091" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<int> val{1000,900,500,400,100,90,50,40,10,9,5,4,1};\n vector<string> sym{\"M\",\"CM\",\"D\",\"CD\",\"C\",\"XC\",\"L\",\"XL\",\"X\", \n \"IX\", \n \"V\",\"IV\",\"I\"};\n\n string result=\"\";\n if(num==0){\n return \"0\";\n }\n\n for(int i=0;i<val.size();i++){\n if(val[i]<=num){\n int times=num/val[i];\n while(times--){\n result+=sym[i];\n }\n num=num%val[i];\n }\n }\n return result;\n }\n};", "memory": "13091" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (num)\n {\n cout<<it->second<<\" \"<<num<<\" \"<<*ans<<endl;\n if (num/it->first)\n {\n if (num/it->first==9)\n {\n ittemp = it;\n --(--ittemp);\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*9;\n ++(++it);\n }\n else if (num/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*4;\n ++(++it);\n }\n else if (num/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n num-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n num-=it->first;\n }\n }\n else ++(++it);\n }\n return *ans;\n }\n};", "memory": "13218" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (num)\n {\n if (num/it->first)\n {\n if (num/it->first==9)\n {\n ittemp = it;\n --(--ittemp);\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*9;\n ++(++it);\n }\n else if (num/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*4;\n ++(++it);\n }\n else if (num/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n num-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n num-=it->first;\n }\n }\n else ++(++it);\n }\n return *ans;\n }\n};", "memory": "13218" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (num)\n {\n if (num/it->first)\n {\n if (num/it->first==9)\n {\n ittemp = it;\n --ittemp;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*9;\n ++it;\n ++it;\n }\n else if (num/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*4;\n ++it;\n ++it;\n }\n else if (num/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n num-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n num-=it->first;\n }\n }\n else \n {\n ++it;\n ++it;\n }\n }\n string out = *ans;\n delete ans;\n return out;\n }\n};", "memory": "13344" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (num)\n {\n if (num/it->first)\n {\n if (num/it->first==9)\n {\n ittemp = it;\n --ittemp;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*9;\n ++it;\n ++it;\n }\n else if (num/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n num-=it->first*4;\n ++it;\n ++it;\n }\n else if (num/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n num-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n num-=it->first;\n }\n }\n else \n {\n ++it;\n ++it;\n }\n }\n string out = *ans;\n delete ans;\n return out;\n }\n};", "memory": "13344" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n int* x = new int(num);\n string* ans = new string;\n map<short,char> rom;\n rom[1]='I';\n rom[5]='V';\n rom[10]='X';\n rom[50]='L';\n rom[100]='C';\n rom[500]='D';\n rom[1000]='M';\n map<short,char>::reverse_iterator it = rom.rbegin();\n auto ittemp = it;\n while (*x)\n {\n if (*x/it->first)\n {\n if (*x/it->first==9)\n {\n ittemp = it;\n --(--ittemp);\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n *x-=it->first*9;\n ++(++it);\n }\n else if (*x/it->first==4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(it->second);\n ans->push_back(ittemp->second);\n *x-=it->first*4;\n ++(++it);\n }\n else if (*x/it->first>4)\n {\n ittemp = it;\n --ittemp;\n ans->push_back(ittemp->second);\n *x-=ittemp->first;\n }\n else\n {\n ans->push_back(it->second);\n *x-=it->first;\n }\n }\n else ++(++it);\n }\n delete x;\n string out = *ans;\n delete ans;\n return out;\n }\n};", "memory": "13470" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\n public:\n string intToRoman(int num) {\n const vector<pair<int, string>> valueSymbols{\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n string ans;\n\n for (const auto& [value, symbol] : valueSymbols) {\n if (num == 0)\n break;\n while (num >= value) {\n num -= value;\n ans += symbol;\n }\n }\n\n return ans;\n }\n};", "memory": "13470" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\n public:\n string intToRoman(int num) {\n const vector<pair<int, string>> valueSymbols{\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n string ans;\n\n for (const auto& [value, symbol] : valueSymbols) {\n if (num == 0)\n break;\n while (num >= value) {\n num -= value;\n ans += symbol;\n }\n }\n\n return ans;\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n \n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n", "memory": "13596" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n // Vector of pairs mapping integer values to their corresponding Roman numeral symbols\n vector<pair<int, string>> valueToRoman = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"},\n {100, \"C\"}, {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"},\n {10, \"X\"}, {9, \"IX\"}, {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}\n };\n \n // Result string to build the Roman numeral representation\n string result = \"\";\n \n // Iterate over each value-symbol pair\n for (const auto& pair : valueToRoman) {\n int value = pair.first; // Integer value of the Roman numeral\n const string& roman = pair.second; // Corresponding Roman numeral symbol\n \n // While the number is greater than or equal to the value\n while (num >= value) {\n result += roman; // Append the Roman numeral symbol to the result\n num -= value; // Decrease the number by the value\n }\n }\n \n return result; // Return the final Roman numeral representation\n }\n};", "memory": "13596" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n // Mapping of integer values to their corresponding Roman numeral symbols\n vector<pair<int, string>> values = {\n {1000, \"M\"},\n {900, \"CM\"},\n {500, \"D\"},\n {400, \"CD\"},\n {100, \"C\"},\n {90, \"XC\"},\n {50, \"L\"},\n {40, \"XL\"},\n {10, \"X\"},\n {9, \"IX\"},\n {5, \"V\"},\n {4, \"IV\"},\n {1, \"I\"}\n };\n \n string result;\n \n // Iterate through the values in descending order\n for (const auto& [value, symbol] : values) {\n // While the number is greater than or equal to the value\n while (num >= value) {\n // Append the symbol to the result\n result += symbol;\n // Subtract the value from the number\n num -= value;\n }\n }\n \n return result;\n }\n};\n", "memory": "13723" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<pair<int, string>> list;\n fillList(list); // from large to small\n\n string res = \"\";\n for (int i = 0; i < list.size(); ++i)\n {\n while (num >= list[i].first)\n {\n res += list[i].second;\n num -= list[i].first;\n }\n }\n return res;\n }\nprivate:\n void fillList(vector<pair<int, string>>& list)\n {\n list = {\n {1000, \"M\"},\n {900, \"CM\"},\n {500, \"D\"},\n {400, \"CD\"},\n {100, \"C\"},\n {90, \"XC\"},\n {50, \"L\"},\n {40, \"XL\"},\n {10, \"X\"},\n {9, \"IX\"},\n {5, \"V\"},\n {4, \"IV\"},\n {1, \"I\"}\n };\n }\n};", "memory": "14228" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int,string> map;\npublic:\n Solution(){\n map[0] = 'I';\n map[1] = 'V';\n map[2] = 'X';\n map[3] = 'L';\n map[4] = 'C';\n map[5] = 'D';\t\n map[6] = 'M';\t\n }\n string intToRoman(int num) {\n string ans = \"\";\n int i = 0;\n while(num > 0){\n int tenth = num % 10;\n if(tenth >= 5){\n string addFront = \"\";\n if(tenth == 9) addFront = map[i] + map[i+2];\n else{\n addFront = map[i+1];\n for(int j = 0; j < tenth % 5; j++){\n addFront += map[i];\n }\n }\n ans = addFront + ans;\n }else{\n string addFront = \"\";\n if(tenth == 4) addFront = map[i] + map[i+1];\n else{\n addFront = \"\";\n for(int j = 0; j < tenth % 5; j++){\n addFront += map[i];\n }\n }\n ans = addFront + ans;\n }\n i+=2;\n num /= 10;\n }\n return ans;\n }\n};", "memory": "14354" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int, string> m = {\n {1, \"I\"},\n {5, \"V\"},\n {10, \"X\"},\n {50, \"L\"},\n {100, \"C\"},\n {500, \"D\"},\n {1000, \"M\"}\n };\n string res = \"\";\n for (auto it = m.rbegin(); it != m.rend(); ++it) {\n if (it->first == 0) break;\n int cur = num / it->first;\n num %= it->first;\n if (cur > 0) {\n auto next = it;\n next++;\n if (cur == 1 && num / next->first == 4) {\n res += m[it->first / 5] + m[it->first * 2];\n num %= it->first;\n num -= 4 * it->first / 5;\n }\n else if (cur == 4) {\n res += it->second + m[it->first * 5];\n } else {\n for (int i = 0; i < cur; ++i) {\n res += it->second;\n }\n }\n }\n }\n return res;\n }\n};", "memory": "14354" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int,string> mp{{1,\"I\"},{5,\"V\"},{10,\"X\"},{50,\"L\"},{100,\"C\"},{500,\"D\"},{1000,\"M\"}};\n\n int temp=num;\n int fac=1;\n string ans=\"\";\n while(temp>0){\n int rem=temp%10;\n if(mp.count(rem*fac)){\n ans=mp[rem*fac]+ans;\n }\n else if(rem*fac==(fac*10)-fac){\n string before=mp[fac];\n string after=mp[fac*10];\n cout<<before;\n cout<<after;\n \n ans=before+after+ans;\n }\n else if(rem*fac==(fac*5)-fac){\n string before=mp[fac];\n string after=mp[fac*5];\n ans=before+after+ans;\n }\n else{\n string numb=\"\";\n if(rem*fac<fac*4){\n for(int i=0;i<rem;i++){\n numb+=mp[fac];\n }\n ans=numb+ans;\n }\n else if(rem*fac>fac*5){\n int extra=((rem*fac)-fac*5)/fac;\n numb+=mp[fac*5];\n for(int i=0;i<extra;i++){\n numb+=mp[fac];\n }\n ans=numb+ans;\n }\n\n }\n fac*=10;\n temp/=10;\n }\n return ans;\n \n }\n};", "memory": "14480" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\n unordered_map<int, string> mp;\n\n void solve(string &roman, int num, int m){\n int h = num/m;\n if(h == 9) roman += mp[m]+mp[m*10];\n else if(h == 4) roman += mp[m]+mp[m*5];\n else if(h > 4 && h < 9){roman += mp[m*5]; int k=h-5; while(k--) roman += mp[m];}\n else while(h--) roman += mp[m];\n }\npublic:\n string intToRoman(int num) {\n mp = {{0, \"\"}, {1,\"I\"}, {5, \"V\"}, {10, \"X\"}, {50, \"L\"}, {100, \"C\"}, {500, \"D\"}, {1000, \"M\"}} ;\n string roman = \"\";\n int i = 3;\n while(i>=0){\n solve(roman, num, pow(10, i));\n num = num%(int)(pow(10, i));\n i--;\n }\n return roman;\n }\n};", "memory": "14606" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // vector<pair<int, char>> mp = {{1000, 'M'}, {500, 'D'}, {100, 'C'},\n // {50, 'L'}, {10, 'X'}, {5, 'V'},\n // {1, 'I'}};\n vector<pair<int, string>> mp = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n\n string intToRoman(int num) {\n string ans = \"\";\n int temp;\n if (num == 0) {\n return ans;\n }\n for (auto i : mp) {\n if (num >= i.first) {\n temp = num % i.first;\n for (int j = 0; j < (num / i.first); j++) {\n ans += i.second;\n // cout << ans << \":\" << num << \":\" << temp << endl;\n }\n break;\n }\n }\n\n return ans + intToRoman(temp);\n }\n};", "memory": "14733" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // vector<pair<int, char>> mp = {{1000, 'M'}, {500, 'D'}, {100, 'C'},\n // {50, 'L'}, {10, 'X'}, {5, 'V'},\n // {1, 'I'}};\n vector<pair<int, string>> mp = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n\n string intToRoman(int num) {\n string ans = \"\";\n int temp;\n if (num == 0) {\n return ans;\n }\n for (auto i : mp) {\n if (num >= i.first) {\n temp = num % i.first;\n for (int j = 0; j < (num / i.first); j++) {\n ans += i.second;\n cout << ans << \":\" << num << \":\" << temp << endl;\n }\n break;\n }\n }\n\n return ans + intToRoman(temp);\n }\n};", "memory": "14859" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n // vector<pair<int, char>> mp = {{1000, 'M'}, {500, 'D'}, {100, 'C'},\n // {50, 'L'}, {10, 'X'}, {5, 'V'},\n // {1, 'I'}};\n vector<pair<int, string>> mp = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n\n string intToRoman(int num) {\n string ans = \"\";\n int temp;\n if (num == 0) {\n return ans;\n }\n for (auto i : mp) {\n if (num >= i.first) {\n temp = num % i.first;\n for (int j = 0; j < (num / i.first); j++) {\n ans += i.second;\n cout << ans << \":\" << num << \":\" << temp << endl;\n }\n break;\n }\n }\n\n return ans + intToRoman(temp);\n }\n};", "memory": "14859" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n unordered_map<int,string> n;\n n[1]=\"I\";\n n[4]=\"IV\";\n n[5]=\"V\";\n n[9]=\"IX\";\n n[10]=\"X\";\n n[40]=\"XL\";\n n[50]=\"L\";\n n[90]=\"XC\";\n n[100]=\"C\";\n n[400]=\"CD\";\n n[500]=\"D\";\n n[900]=\"CM\";\n n[1000]=\"M\";\n string roman = \"\";\n for (int pair : {1000,900,500,400,100,90,50,40,10,9,5,4,1}) {\n while(num>=pair){\n roman += n[pair];\n num -= pair;\n }\n }\n return roman;\n }\n};", "memory": "14985" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n\n std::map<int, std::tuple<int, char, char>> romans;\n romans[0] = std::make_tuple(1000, 'M', '\\0');\n romans[1] = std::make_tuple(900, 'C', 'M');\n romans[2] = std::make_tuple(500, 'D', '\\0');\n romans[3] = std::make_tuple(400, 'C', 'D');\n romans[4] = std::make_tuple(100, 'C', '\\0');\n romans[5] = std::make_tuple(90, 'X', 'C');\n romans[6] = std::make_tuple(50, 'L', '\\0');\n romans[7] = std::make_tuple(40, 'X', 'L');\n romans[8] = std::make_tuple(10, 'X', '\\0');\n romans[9] = std::make_tuple(9, 'I', 'X');\n romans[10] = std::make_tuple(5, 'V', '\\0');\n romans[11] = std::make_tuple(4, 'I', 'V');\n romans[12] = std::make_tuple(1, 'I', '\\0');\n\n string output;\n\n for(int i = 0; i < 13; i++)\n {\n tuple x = romans[i];\n while(num >= get<0>(x))\n {\n output += get<1>(x);\n if (get<2>(x) != '\\0')\n {\n output += get<2>(x);\n }\n num -= get<0>(x);\n }\n }\n\n return output;\n }\n};", "memory": "14985" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n unordered_map<int, string> m;\n m[1] = \"I\";\n m[4] = \"IV\";\n m[5] = \"V\";\n m[9] = \"IX\";\n m[10] = \"X\";\n m[40] = \"XL\";\n m[50] = \"L\";\n m[90] = \"XC\";\n m[100] = \"C\";\n m[400] = \"CD\";\n m[500] = \"D\";\n m[900] = \"CM\";\n m[1000] = \"M\";\n string ans = \"\";\n int x = 0;\n while (num) {\n if (num >= 1000)\n x = 1000;\n else if (num >= 900)\n x = 900;\n else if (num >= 500)\n x = 500;\n else if (num >= 400)\n x = 400;\n else if (num >= 100)\n x = 100;\n else if (num >= 90)\n x = 90;\n else if (num >= 50)\n x = 50;\n else if (num >= 40)\n x = 40;\n else if (num >= 10)\n x = 10;\n else if (num >= 9)\n x = 9;\n else if (num >= 5)\n x = 5;\n else if (num >= 4)\n x = 4;\n else\n x = 1;\n ans += m[x];\n num -= x;\n }\n return ans;\n }\n};", "memory": "15111" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n \n unordered_map<int, string> umap;\n umap[4] = \"IV\";\n umap[9] = \"IX\";\n umap[40] = \"XL\";\n umap[90] = \"XC\";\n umap[400] = \"CD\";\n umap[900] = \"CM\";\n umap[1] = \"I\";\n umap[5] = \"V\";\n umap[10] = \"X\";\n umap[50] = \"L\";\n umap[100] = \"C\";\n umap[500] = \"D\";\n umap[1000] = \"M\";\n\n string res = \"\";\n\n int i = 1000;\n while(i > 0.99){ \n \n int val = num / i;\n num = num % i;\n \n // std::cout << \"_______________\" << std::endl;\n // std::cout << i << std::endl;\n // std::cout << \"val: \";\n // std::cout << val << std::endl;\n // std::cout << \"num: \";\n // std::cout << num << std::endl;\n // std::cout << umap[val] << std::endl;\n \n if(val == 9 || val == 4){\n res.append(umap[val*i]);\n val = 0;\n }\n if(val >= 5) {\n res.append(umap[5*i]);\n val -= 5;\n }\n\n for(val;val > 0;val--){\n res.append(umap[i]);\n }\n i /= 10;\n }\n return res;\n }\n};", "memory": "15238" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n unordered_map<int, string> m;\n m[1] = \"I\";\n m[4] = \"IV\";\n m[5] = \"V\";\n m[9] = \"IX\";\n m[10] = \"X\";\n m[40] = \"XL\";\n m[50] = \"L\";\n m[90] = \"XC\";\n m[100] = \"C\";\n m[400] = \"CD\";\n m[500] = \"D\";\n m[900] = \"CM\";\n m[1000] = \"M\";\n string ans = \"\";\n int x = 0;\n while (num) {\n if (num >= 1000)\n x = 1000;\n else if (num >= 900)\n x = 900;\n else if (num >= 500)\n x = 500;\n else if (num >= 400)\n x = 400;\n else if (num >= 100)\n x = 100;\n else if (num >= 90)\n x = 90;\n else if (num >= 50)\n x = 50;\n else if (num >= 40)\n x = 40;\n else if (num >= 10)\n x = 10;\n else if (num >= 9)\n x = 9;\n else if (num >= 5)\n x = 5;\n else if (num >= 4)\n x = 4;\n else\n x = 1;\n ans += m[x];\n num -= x;\n }\n return ans;\n }\n};", "memory": "15238" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<pair<string, int>> roman = {\n {\"I\", 1}, {\"IV\", 4}, {\"V\", 5}, {\"IX\", 9},\n {\"X\", 10}, {\"XL\", 40}, {\"L\", 50}, {\"XC\", 90},\n {\"C\", 100}, {\"CD\", 400}, {\"D\", 500}, {\"CM\", 900},\n {\"M\", 1000}\n };\n\n string res;\n std::sort(roman.begin(), roman.end(), [](auto a, auto b) {\n return a.second > b.second;\n });\n\n for (auto const& [symbol, number] : roman) {\n if (num / number > 0) {\n int count = num / number;\n while (num >= number) {\n res += symbol;\n num -= number;\n }\n }\n }\n\n return res;\n }\n};", "memory": "15364" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<pair<string, int>> roman = {\n {\"I\", 1}, {\"IV\", 4}, {\"V\", 5}, {\"IX\", 9},\n {\"X\", 10}, {\"XL\", 40}, {\"L\", 50}, {\"XC\", 90},\n {\"C\", 100}, {\"CD\", 400}, {\"D\", 500}, {\"CM\", 900},\n {\"M\", 1000}\n };\n\n string res;\n std::sort(roman.begin(), roman.end(), [](auto a, auto b) {\n return a.second > b.second;\n });\n\n for (auto const& [symbol, number] : roman) {\n if (num / number > 0) {\n int count = num / number;\n while (num >= number) {\n res += symbol;\n num -= number;\n }\n }\n }\n\n return res;\n }\n};", "memory": "15364" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string ans=\"\";\n \n unordered_map<int, string>mp = {\n {1000, \"M\"},\n {900, \"CM\"},\n {500, \"D\"},\n {400, \"CD\"},\n {100, \"C\"},\n {90, \"XC\"},\n {50, \"L\"},\n {40, \"XL\"},\n {10, \"X\"},\n {9, \"IX\"},\n {4, \"IV\"},\n {5, \"V\"},\n {1, \"I\"}\n };\n\n vector<int>v = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n\n\n int k = num;\n while(k > 0){\n for(int i=0; i<13; i++) {\n if(k>=v[i]) {\n ans+=mp[v[i]];\n k-=v[i];\n break;\n }\n }\n }\n\n return ans;\n\n\n\n }\n};", "memory": "15490" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string result;\n\n unordered_map<int, string> values;\n values[1] = \"I\";\n values[4] = \"IV\";\n values[5] = \"V\";\n values[9] = \"IX\";\n values[10] = 'X';\n values[40] = \"XL\";\n values[50] = \"L\";\n values[90] = \"XC\";\n values[100] = \"C\";\n values[400] = \"CD\";\n values[500] = \"D\";\n values[900] = \"CM\";\n values[1000] = \"M\";\n\n vector<int> bases{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n\n for (int base : bases) {\n while (num >= base) {\n result += values[base];\n num -= base;\n }\n }\n\n return result;\n }\n};", "memory": "15616" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nstring intToRoman(int num)\n{\n // Mapping of integer values to their corresponding Roman numerals\n unordered_map<int, string> valueSymbols = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"}, {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"}, {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}};\n\n string result = \"\";\n vector<int> digit;\n\n while (num)\n {\n int val = num % 10;\n digit.push_back(val);\n num /= 10;\n }\n\n int i = 1;\n cout << \"Digits in reverse order: \";\n for (int d : digit)\n {\n if (d >= 5)\n {\n\n if (d == 9)\n {\n\n result = valueSymbols[i] + valueSymbols[i * 10] + result;\n }\n else if (d == 8)\n {\n result = valueSymbols[(i * 10) / 2] + valueSymbols[i] + valueSymbols[i] + valueSymbols[i] + result;\n }\n else if (d == 7)\n {\n result = valueSymbols[(i * 10) / 2] + valueSymbols[i] + valueSymbols[i] + result;\n }\n else if (d == 6)\n {\n result = valueSymbols[(i * 10) / 2] + valueSymbols[i] + result;\n }\n else if (d == 5)\n {\n result = valueSymbols[(i * 10) / 2] + result;\n }\n }\n else{\n if (d == 4)\n {\n\n result = valueSymbols[i] + valueSymbols[(i * 10) / 2] + result;\n }\n else if (d == 3)\n {\n result = valueSymbols[i] + valueSymbols[i] + valueSymbols[i] + result;\n }\n else if (d == 2)\n {\n result = valueSymbols[i] + valueSymbols[i] + result;\n }\n else if (d == 1)\n {\n result = valueSymbols[i] + result;\n }\n // else if (d == 5)\n // {\n // result = valueSymbols[(i * 10) / 2] + result;\n // }\n\n }\n i*=10;\n }\n cout << endl;\n return result;\n}\n};", "memory": "15743" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<pair<int,string>>m1 = {{1,\"I\"},{5,\"V\"},{10,\"X\"},{50,\"L\"},{100,\"C\"},{500,\"D\"},\n {1000,\"M\"},{4,\"IV\"},{9,\"IX\"},{40,\"XL\"},{90,\"XC\"},{400,\"CD\"},{900,\"CM\"}};\n sort(m1.rbegin(),m1.rend());\n string ans;\n for(auto it : m1){\n int times = num/it.first;\n if(times>0){\n for(int i = 0;i<times;i++){\n ans+=it.second;\n }\n num%=it.first;\n }\n }\n return ans;\n }\n};", "memory": "15743" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "#include <string>\n#include <cmath>\n\nclass Solution {\npublic:\n string intToRoman(int num) {\n string rs = \"\";\n \n map<int, string>itr ={\n {1, \"I\"},\n {5, \"V\"},\n {10, \"X\"},\n {50, \"L\"},\n {100, \"C\"},\n {500, \"D\"},\n {1000, \"M\"},\n };\n\n string ns = to_string(num);\n int deci = ns.length();\n int n;\n int x;\n\n for(int i = 0; i< ns.length(); i++){\n cout << \"deci: \" << deci << endl;\n cout << \"ns[i]: \" << stoi(string(1,ns[i]))*pow(10,deci-1) << endl;\n if((ns[i] == '4') || (ns[i] == '9')){\n cout << \"special case: \" << endl;\n n = stoi(string(1, ns[i]));\n\n rs += itr[pow(10,deci-1)] + itr[(n+1)*pow(10,deci-1)];\n }else if((ns[i] != '4') && (ns[i] != '9')){\n cout << \"normal case [current deci: \"<< deci<< \"]\"<<endl;\n n = stoi(string(1, ns[i]));\n if(n > 5){\n rs += itr[5*pow(10,deci-1)];\n x = n - 5;\n for(int j = 0; j < x; j++){\n rs += itr[pow(10,deci-1)];\n }\n }else if(n == 5){\n rs += itr[5*pow(10,deci-1)];\n }else{\n for(int j = 0; j < n; j++){\n rs += itr[pow(10,deci-1)];\n }\n }\n \n }\n cout << \"current rs: \" << rs << endl;\n cout << endl;\n deci--;\n }\n return rs;\n }\n};", "memory": "15869" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> thou = {\"\", \"M\", \"MM\", \"MMM\"};\n vector<string> hund = {\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"};\n vector<string> tens = {\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"};\n vector<string> ones = {\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"};\n\n return thou[num / 1000] + hund[num % 1000 / 100] + tens[num % 100 / 10] + ones[num % 10];\n }\n};", "memory": "15995" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> ones = {\"\",\"I\",\"II\",\"III\",\"IV\",\"V\",\"VI\",\"VII\",\"VIII\",\"IX\"};\n vector<string> tens = {\"\",\"X\",\"XX\",\"XXX\",\"XL\",\"L\",\"LX\",\"LXX\",\"LXXX\",\"XC\"};\n vector<string> hrns = {\"\",\"C\",\"CC\",\"CCC\",\"CD\",\"D\",\"DC\",\"DCC\",\"DCCC\",\"CM\"};\n vector<string> ths = {\"\",\"M\",\"MM\",\"MMM\"};\n\n return ths[num/1000] + hrns[(num%1000)/100] + tens[(num%100)/10] + ones[num%10];\n }\n};", "memory": "16121" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": " class Solution {\n\npublic:\n\n string intToRoman(int num) {\n\n \n\n // pre calculate the roman value\n\n \n\n vector<string> ones = {\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"};\n\n \n\n vector<string> tens = {\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"};\n\n \n\n vector<string> hundreds = {\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"};\n\n \n\n vector<string> thousands = {\"\", \"M\", \"MM\", \"MMM\"};\n\n \n\n string res = \"\";\n\n \n\n // find digit at thousands place\n\n \n\n res += thousands[num / 1000];\n\n \n\n // find digit at hundreds place\n\n \n\n res += hundreds[(num % 1000) / 100];\n\n \n\n // find digit at tens place\n\n \n\n res += tens[(num % 100) / 10];\n\n \n\n // find digit at ones place\n\n \n\n res += ones[num % 10];\n\n \n\n return res;\n\n }\n\n}; \n\n \n \n \n", "memory": "16248" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n unordered_map<int,string> map{\n {1,\"I\"},{5,\"V\"},{10,\"X\"},{50,\"L\"},{100,\"C\"},{500,\"D\"},{1000,\"M\"}\n };\n unordered_map<int,string> map2{\n {4,\"IV\"},{9,\"IX\"},{40,\"XL\"},{90,\"XC\"},{400,\"CD\"},{900,\"CM\"}\n };\n\n vector<int> Roman {1,5,10,50,100,500,1000};\n int index = 6;\n int mul = 1;\n while(true){\n if(num/mul<10){\n break;\n }\n mul = mul *10;\n }\n string str;\n int number;\n\n while(true){\n number = num/mul;\n number = number*mul;\n if(number == 0){\n if(mul == 1){\n break;\n }\n num = num%mul;\n mul = mul/10;\n continue;\n }\n if(number/mul == 4||number/mul == 9){\n\n str+= map2[number];\n }\n else{\n while(true){\n if(Roman[index]>number){\n index--;\n continue;\n }\n str+= map[Roman[index]];\n number-= Roman[index];\n if(number == 0){\n break;\n }\n \n }\n\n\n }\n \n if(mul==1){\n break;\n }\n num = num%mul;\n mul = mul/10;\n }\n return str;\n\n }\n};", "memory": "16248" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\nstring intToRoman(int num) {\n vector<string> thousands = {\"\", \"M\", \"MM\", \"MMM\"};\n vector<string> hundreds = {\n \"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"};\n vector<string> tens = {\n \"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"};\n vector<string> ones = {\n \"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"};\n /*\n thousands_digit = num / 1000 --> 3999 / 1000 = 3\n hundreds_digit = num % 1000 --> 999 / 100 = 9\n tens_digit = (num % 100) = 99 --> 99/10 = 9\n tens_digit = (num % 10) = 9\n */\n\n return thousands[num/1000] + hundreds[(num%1000)/100] + tens[(num%100)/10] + ones[num%10];\n}\n};", "memory": "16374" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\n\npublic:\n\n string intToRoman(int num) {\n\n \n\n // pre calculate the roman value\n\n \n\n vector<string> ones = {\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"};\n\n \n\n vector<string> tens = {\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"};\n\n \n\n vector<string> hundreds = {\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"};\n\n \n\n vector<string> thousands = {\"\", \"M\", \"MM\", \"MMM\"};\n\n \n\n string res = \"\";\n\n \n\n // find digit at thousands place\n\n \n\n res += thousands[num / 1000];\n\n \n\n // find digit at hundreds place\n\n \n\n res += hundreds[(num % 1000) / 100];\n\n \n\n // find digit at tens place\n\n \n\n res += tens[(num % 100) / 10];\n\n \n\n // find digit at ones place\n\n \n\n res += ones[num % 10];\n\n \n\n return res;\n\n }\n\n};\n\n\n \n \n", "memory": "16374" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<string> ones = {\"\",\"I\",\"II\",\"III\",\"IV\",\"V\",\"VI\",\"VII\",\"VIII\",\"IX\"};\n vector<string> tens = {\"\",\"X\",\"XX\",\"XXX\",\"XL\",\"L\",\"LX\",\"LXX\",\"LXXX\",\"XC\"};\n vector<string> hundreds = {\"\",\"C\",\"CC\",\"CCC\",\"CD\",\"D\",\"DC\",\"DCC\",\"DCCC\",\"CM\"};\n vector<string> thousands = {\"\",\"M\",\"MM\",\"MMM\",\"MMMM\"};\n \n return thousands[num/1000]+hundreds[(num%1000)/100]+tens[(num%100)/10]\n +ones[(num%10)];\n \n \n }\n};", "memory": "16500" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\n vector<pair<string,int>> v= {{\"I\", 1}, {\"IV\", 4}, {\"V\", 5}, {\"IX\",9}, {\"X\", 10}, {\"XL\", 40}, {\"L\", 50}, {\"XC\",90}, {\"C\", 100}, {\"CD\", 400}, {\"D\", 500}, {\"CM\", 900}, {\"M\", 1000}};\n\n string getRepresentation(int x)\n { \n if (x == 0)\n return \"\";\n int i = 0;\n for (; i<v.size(); i++)\n {\n if (v[i].second>x)\n {\n break;\n }\n }\n\n int sum = x-v[i-1].second;\n return v[i-1].first + getRepresentation(sum);\n }\n\npublic:\n string intToRoman(int num) {\n \n int k = to_string(num).size()-1;\n string res=\"\";\n while(k>=0)\n {\n int m = pow(10,k);\n int x = (num/m) * m;\n num = num % m;\n res += getRepresentation(x);\n k--;\n }\n\n return res;\n }\n};", "memory": "16626" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n unordered_map <int, char> map;\n unordered_map <int, string> subtractive_map;\n\n vector <int> fixed_values = {1000, 500, 100, 50, 10, 5, 1};\n vector <int> subtractive_values = {900, 400, 90, 40, 9, 4};\n\npublic:\n Solution() {\n map[1] = 'I';\n map[5] = 'V';\n map[10] = 'X';\n map[50] = 'L';\n map[100] = 'C';\n map[500] = 'D';\n map[1000] = 'M';\n\n subtractive_map[900] = \"CM\";\n subtractive_map[400] = \"CD\";\n subtractive_map[90] = \"XC\";\n subtractive_map[40] = \"XL\";\n subtractive_map[9] = \"IX\";\n subtractive_map[4] = \"IV\";\n }\n\n string intToRoman(int num) {\n string output;\n\n // First find the number of digits in the number\n int divisor = (int)pow(10.0, floor(log10(num)));\n\n // Now start on the left most digit\n while (divisor != 0) {\n int digit = num / divisor;\n num %= divisor;\n\n int current_value = digit * divisor;\n while (current_value > 0) {\n switch (digit) {\n case 4:\n case 9:\n for (int value : subtractive_values) {\n if (current_value >= value) {\n output += subtractive_map[value];\n current_value -= value;\n break;\n }\n }\n break;\n default:\n for (int value : fixed_values) {\n if (current_value >= value) {\n output += map[value];\n current_value -= value;\n break;\n }\n }\n }\n }\n divisor /= 10;\n }\n return output;\n }\n};\n", "memory": "16753" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int, string> m{\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"}, {100, \"C\"},\n {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"}, {10, \"X\"}, {9, \"IX\"},\n {5, \"V\"}, {4, \"IV\"}, {1, \"I\"},\n };\n string res = \"\";\n for (auto i = m.rbegin(); i != m.rend(); ++i) {\n if (int tmp = num / (i->first); tmp) {\n for (int j = 0; j < tmp; ++j)\n res += (i->second);\n num %= (i->first);\n }\n }\n return res;\n }\n};", "memory": "16753" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n\n map<int, string> romans;\n romans.insert({1000, \"M\"});\n romans.insert({900, \"CM\"});\n romans.insert({500, \"D\"});\n romans.insert({400, \"CD\"});\n romans.insert({100, \"C\"});\n romans.insert({90, \"XC\"});\n romans.insert({50, \"L\"});\n romans.insert({40, \"XL\"});\n romans.insert({10, \"X\"});\n romans.insert({9, \"IX\"});\n romans.insert({5, \"V\"});\n romans.insert({4, \"IV\"});\n romans.insert({1, \"I\"});\n\n string output;\n\n for (auto iter = romans.rbegin(); iter != romans.rend(); ++iter)\n {\n while(num >= iter->first)\n {\n output += iter->second;\n\n num -= iter->first;\n }\n }\n\n return output;\n }\n};", "memory": "16879" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n int number=num;\n string roman =\"\";\n map<int ,string> m ={{1,\"I\"},{4,\"IV\"},{5,\"V\"},{9,\"IX\"},{10,\"X\"},{40,\"XL\"},{50,\"L\"},{90,\"XC\"},{100,\"C\"},{400,\"CD\"},{500,\"D\"},{900,\"CM\"},{1000,\"M\"}};\n\n \n for (auto it =m.rbegin();it!=m.rend()&&num>0;it++){\n if(num>=it->first){\n int freq=num/it->first;\n while (freq--){\n roman +=it->second;\n }\n }else{\n\n }\n num=num%it->first;\n }\n return roman;\n \n }\n};", "memory": "17005" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int, string> mpp = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"},\n {100, \"C\"}, {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"},\n {10, \"X\"}, {9, \"IX\"}, {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}\n };\n\n string result = \"\";\n for(auto it = mpp.rbegin(); it != mpp.rend(); ++it)\n {\n while(num>=it->first)\n {\n num-=it->first;\n result+=it->second;\n }\n }\n return result;\n }\n};", "memory": "17005" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int, string> mpp = {\n {1000, \"M\"}, {900, \"CM\"}, {500, \"D\"}, {400, \"CD\"},\n {100, \"C\"}, {90, \"XC\"}, {50, \"L\"}, {40, \"XL\"},\n {10, \"X\"}, {9, \"IX\"}, {5, \"V\"}, {4, \"IV\"}, {1, \"I\"}\n };\n\n string result = \"\";\n for(auto it = mpp.rbegin(); it != mpp.rend(); ++it)\n {\n while(num>=it->first)\n {\n num-=it->first;\n result+=it->second;\n }\n }\n return result;\n }\n};", "memory": "17131" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n // Define the Roman numeral map in descending order of values\n map<int, string> mymap = {\n {1, \"I\"}, {4, \"IV\"}, {5, \"V\"}, {9, \"IX\"}, {10, \"X\"}, {40, \"XL\"},\n {50, \"L\"}, {90, \"XC\"}, {100, \"C\"}, {400, \"CD\"}, {500, \"D\"},\n {900, \"CM\"}, {1000, \"M\"}\n };\n\n\n string ans = \"\";\n\n for (auto it = mymap.rbegin(); it != mymap.rend(); ++it) {\n int times = num / it->first;\n\n for (int i = 0; i < times; i++) {\n ans += it->second;\n }\n\n num = num % it->first; \n }\n\n return ans;\n }\n};", "memory": "17131" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string s = \"\";\n\n map<long, string> mp = {\n {1, \"I\"},\n {4, \"IV\"},\n {5, \"V\"},\n {9, \"IX\"},\n {10, \"X\"},\n {40, \"XL\"},\n {50, \"L\"},\n {90, \"XC\"},\n {100, \"C\"},\n {400, \"CD\"},\n {500, \"D\"},\n {900, \"CM\"},\n {1000, \"M\"}};\n\n for (auto it = mp.rbegin(); it != mp.rend(); it++)\n {\n while (num - (*it).first >= 0)\n {\n s += (*it).second;\n num -= (*it).first;\n }\n }\n return s;\n }\n};", "memory": "17258" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n vector<pair<int,string>> v;\n string s=\"\";\n string alpha;\n int ans=0;\n v.emplace_back(1,\"I\");\n v.emplace_back(4,\"IV\");\n v.emplace_back(5,\"V\");\n v.emplace_back(9,\"IX\");\n v.emplace_back(10,\"X\");\n v.emplace_back(40,\"XL\");\n v.emplace_back(50,\"L\");\n v.emplace_back(90,\"XC\");\n v.emplace_back(100,\"C\");\n v.emplace_back(400,\"CD\");\n v.emplace_back(500,\"D\");\n v.emplace_back(900,\"CM\");\n v.emplace_back(1000,\"M\");\n int n=v.size();\n while(num>0)\n {\n for(int i=0;i<n;i++)\n {\n if(v[i].first<=num)\n {\n ans=v[i].first;\n alpha=v[i].second;\n }\n else\n {\n break;\n }\n }\n num=num-ans;\n s+=alpha;\n }\n return s;\n }\n};", "memory": "17258" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n string s=\"\";\n map<int,string>mp={{1,\"I\"},{4,\"IV\"},{5,\"V\"},{9,\"IX\"},{10,\"X\"},{40,\"XL\"},{50,\"L\"},{90,\"XC\"},{100,\"C\"},{400, \"CD\"},{500,\"D\"},{900,\"CM\"},{1000,\"M\"}};\n\n while(num!=0){\n if(num>=1000){\n s+=mp[1000];\n num-=1000;\n }\n else if(num>=900){\n s+=mp[900];num-=900;\n }\n else if(num>=500){\n s+=mp[500];\n num-=500;\n }\n else if(num>=400){\n s+=mp[400];num-=400;\n }\n else if(num>=100){\n s+=mp[100];\n num-=100;\n }\n else if(num>=90){\n s+=mp[90];num-=90;\n }\n else if(num>=50){\n s+=mp[50];\n num-=50;\n }\n else if(num>=40){\n s+=mp[40];num-=40;\n }\n else if(num>=10){\n s+=mp[10];\n num-=10;\n }\n else if(num==9){\n s+=mp[9];num-=9;\n }\n else if(num>=5){\n s+=mp[5];num-=5;\n }\n else if(num==4){\n s+=mp[4];num-=4;\n }\n else if(num>=1){\n s+=mp[1];num-=1;\n }\n }\n\n return s;\n }\n};", "memory": "17384" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "#include <map>\n\nenum romano {\n I = 1,\n IV = 4,\n V = 5,\n IX = 9,\n X = 10,\n XL = 40,\n L = 50,\n XC = 90,\n C = 100,\n CD = 400,\n D = 500,\n CM = 900,\n M = 1000,\n};\n\nclass Solution {\nprivate:\n std::map<romano, std::string> mymap = {\n {I,\"I\"},\n {IV, \"IV\"},\n {V, \"V\"},\n {IX, \"IX\"},\n {X, \"X\"},\n {XL, \"XL\"},\n {L, \"L\"},\n {XC, \"XC\"},\n {C, \"C\"},\n {CD, \"CD\"},\n {D, \"D\"},\n {CM, \"CM\"},\n {M, \"M\"},\n };\npublic:\n string intToRoman(int num) {\n string val;\n long long tmp = num;\n long long dig = 0;\n for (auto iter = mymap.rbegin(); iter != mymap.rend(); ++iter) {\n //std::cout << iter->first << \": \" << iter->second << std::endl;\n dig = tmp/iter->first;\n for (int i=0; i < dig; i++) {\n val += iter->second;\n }\n tmp = tmp%iter->first;\n } \n return val;\n }\n};", "memory": "17384" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string intToRoman(int num) {\n map<int, string> sub;\n map<int, string> dig = {{1, \"I\"}, {2, \"V\"}, {3, \"X\"}, {4, \"L\"}, {5, \"C\"}, {6, \"D\"}, {7, \"M\"}};\n sub.insert({1, \"VI\"});\n sub.insert({2, \"XI\"});\n sub.insert({3, \"LX\"});\n sub.insert({4, \"CX\"});\n sub.insert({5, \"DC\"});\n sub.insert({6, \"MC\"});\n\n string ret;\n int i = 1;\n while (num > 0) {\n int digit = num % 10;\n if (digit != 0) {\n if (digit == 4) ret += sub[i];\n else if (digit == 9) ret += sub[i+1];\n else {\n if (digit < 5) {\n for (int j = 0; j < digit; j++) ret += dig[i];\n } else {\n for (int j = 5; j < digit; j++) ret += dig[i];\n ret += dig[i+1];\n }\n }\n }\n\n num /= 10;\n i+=2;\n }\n\n reverse(ret.begin(), ret.end());\n return ret;\n }\n};", "memory": "17510" }
12
<p>Seven different symbols represent Roman numerals with the following values:</p> <table> <thead> <tr> <th>Symbol</th> <th>Value</th> </tr> </thead> <tbody> <tr> <td>I</td> <td>1</td> </tr> <tr> <td>V</td> <td>5</td> </tr> <tr> <td>X</td> <td>10</td> </tr> <tr> <td>L</td> <td>50</td> </tr> <tr> <td>C</td> <td>100</td> </tr> <tr> <td>D</td> <td>500</td> </tr> <tr> <td>M</td> <td>1000</td> </tr> </tbody> </table> <p>Roman numerals are formed by appending&nbsp;the conversions of&nbsp;decimal place values&nbsp;from highest to lowest. Converting a decimal place value into a Roman numeral has the following rules:</p> <ul> <li>If the value does not start with 4 or&nbsp;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> <li>If the value starts with 4 or 9 use the&nbsp;<strong>subtractive form</strong>&nbsp;representing&nbsp;one symbol subtracted from the following symbol, for example,&nbsp;4 is 1 (<code>I</code>) less than 5 (<code>V</code>): <code>IV</code>&nbsp;and 9 is 1 (<code>I</code>) less than 10 (<code>X</code>): <code>IX</code>.&nbsp;Only the following subtractive forms are used: 4 (<code>IV</code>), 9 (<code>IX</code>),&nbsp;40 (<code>XL</code>), 90 (<code>XC</code>), 400 (<code>CD</code>) and 900 (<code>CM</code>).</li> <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&nbsp;(<code>V</code>), 50 (<code>L</code>), or 500 (<code>D</code>) multiple times. If you need to append a symbol&nbsp;4 times&nbsp;use the <strong>subtractive form</strong>.</li> </ul> <p>Given an integer, convert it to a Roman numeral.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 3749</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MMMDCCXLIX&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M) 700 = DCC as 500 (D) + 100 (C) + 100 (C) 40 = XL as 10 (X) less of 50 (L) 9 = IX as 1 (I) less of 10 (X) Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 58</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;LVIII&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 50 = L 8 = VIII </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">num = 1994</span></p> <p><strong>Output:</strong> <span class="example-io">&quot;MCMXCIV&quot;</span></p> <p><strong>Explanation:</strong></p> <pre> 1000 = M 900 = CM 90 = XC 4 = IV </pre> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= num &lt;= 3999</code></li> </ul>
3
{ "code": "class Solution {\n\npublic:\n\n string intToRoman(int num) {\n\n map<int, string> ans;\n\n ans[1000] = \"M\";\n\n ans[900] = \"CM\";\n\n ans[500] = \"D\";\n\n ans[400] = \"CD\";\n\n ans[100] = \"C\";\n\n ans[90] = \"XC\";\n\n ans[50] = \"L\";\n\n ans[40] = \"XL\";\n\n ans[10] = \"X\";\n\n ans[9] = \"IX\";\n\n ans[5] = \"V\";\n\n ans[4] = \"IV\";\n\n ans[1] = \"I\";\n\n string res = \"\";\n\n for (auto i = ans.rbegin(); i != ans.rend(); ++i) {\n\n int a = i->first;\n\n string b = i->second;\n\n while (num >= a) {\n\n res += b;\n\n num -= a;\n\n }\n\n }\n\n return res;\n\n }\n\n};\n\n \n \n \n \n \n", "memory": "17636" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\n constexpr inline u_short decode(const char c) const {\n switch (c) {\n case 'I':\n return 1;\n case 'V':\n return 5;\n case 'X':\n return 10;\n case 'L':\n return 50;\n case 'C':\n return 100;\n case 'D':\n return 500;\n case 'M':\n return 1000;\n }\n return 0;\n }\n\npublic:\n constexpr inline u_short romanToInt(string_view data) const {\n u_short result = 0;\n u_short tmp = 0;\n u_short previous = 2000;\n u_short current = 0;\n\n for (char c : data) {\n if (c != '\"') {\n current = decode(c);\n if (current > previous) {\n result += current - tmp;\n tmp = 0;\n } else if (current < previous) {\n result += tmp;\n tmp = current;\n } else\n tmp += current;\n previous = current;\n }\n }\n return result + tmp;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.romanToInt(line);\n\n exit(0);\n return 0;\n}();", "memory": "7200" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\n constexpr inline u_short decode(const char c) const {\n switch (c) {\n case 'I':\n return 1;\n case 'V':\n return 5;\n case 'X':\n return 10;\n case 'L':\n return 50;\n case 'C':\n return 100;\n case 'D':\n return 500;\n case 'M':\n return 1000;\n }\n return 0;\n }\n\npublic:\n constexpr inline u_short romanToInt(string_view data) const {\n u_short result = 0;\n u_short tmp = 0;\n u_short previous = 2000;\n u_short current = 0;\n\n for (char c : data) {\n if (c != '\"') {\n current = decode(c);\n if (current > previous) {\n result += current - tmp;\n tmp = 0;\n } else if (current < previous) {\n result += tmp;\n tmp = current;\n } else\n tmp += current;\n previous = current;\n }\n }\n return result + tmp;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.romanToInt(line);\n\n exit(0);\n return 0;\n}();", "memory": "7400" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\n constexpr inline u_short decode(const char c) const {\n switch (c) {\n case 'I':\n return 1;\n case 'V':\n return 5;\n case 'X':\n return 10;\n case 'L':\n return 50;\n case 'C':\n return 100;\n case 'D':\n return 500;\n case 'M':\n return 1000;\n }\n return 0;\n }\n\npublic:\n constexpr inline u_short romanToInt(string_view data) const {\n u_short result = 0;\n u_short tmp = 0;\n u_short previous = 2000;\n u_short current = 0;\n\n for (char c : data) {\n if (c != '\"') {\n current = decode(c);\n if (current > previous) {\n result += current - tmp;\n tmp = 0;\n } else if (current < previous) {\n result += tmp;\n tmp = current;\n } else\n tmp += current;\n previous = current;\n }\n }\n return result + tmp;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.romanToInt(line);\n\n exit(0);\n return 0;\n}();", "memory": "7500" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\n constexpr inline u_short decode(const char c) const {\n switch (c) {\n case 'I':\n return 1;\n case 'V':\n return 5;\n case 'X':\n return 10;\n case 'L':\n return 50;\n case 'C':\n return 100;\n case 'D':\n return 500;\n case 'M':\n return 1000;\n }\n return 0;\n }\n\npublic:\n constexpr inline u_short romanToInt(string_view data) const {\n u_short result = 0;\n u_short tmp = 0;\n u_short previous = 2000;\n u_short current = 0;\n\n for (char c : data) {\n if (c != '\"') {\n current = decode(c);\n if (current > previous) {\n result += current - tmp;\n tmp = 0;\n } else if (current < previous) {\n result += tmp;\n tmp = current;\n } else\n tmp += current;\n previous = current;\n }\n }\n return result + tmp;\n }\n};\n\nint init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n\n ofstream out(\"user.out\");\n cout.rdbuf(out.rdbuf());\n\n Solution s;\n for (string line; getline(cin, line); cout << endl)\n cout << s.romanToInt(line);\n\n exit(0);\n return 0;\n}();", "memory": "7500" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int priority(char c){\n if(c == 'M') return 7;\n else if (c == 'D') return 6;\n else if (c == 'C') return 5;\n else if (c == 'L') return 4;\n else if (c == 'X') return 3;\n else if (c == 'V') return 2;\n else return 1;\n }\n int value(int x){\n if(x == 7) return 1000;\n else if(x == 6) return 500;\n else if(x == 5) return 100;\n else if(x == 4) return 50;\n else if(x == 3) return 10;\n else if (x == 2) return 5;\n else return 1;\n }\n\n int romanToInt(string s) {\n int pp = 8;\n int cp = 0;\n int ans = 0;\n for(auto it: s){\n cp = priority(it);\n if(pp < cp){\n ans += value(cp);\n ans -= 2*value(pp);\n }\n else {\n ans += value(cp);\n }\n pp = cp;\n }\n return ans;\n }\n};", "memory": "10400" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int result = 0, i = 0;\n while (s[i] != '\\0') {\n if (s[i] == 'M') {\n result += 1000;\n }\n if (s[i] == 'C') {\n if (s[i + 1] == 'D') {\n result += 400;\n i += 2;\n } else if (s[i + 1] == 'M') {\n result += 900;\n i += 2;\n } else\n result += 100;\n }\n if (s[i] == 'D')\n result += 500;\n if (s[i] == 'X') {\n if (s[i + 1] == 'C') {\n result += 90;\n i += 2;\n } else if (s[i + 1] == 'L') {\n result += 40;\n i += 2;\n } else\n result += 10;\n }\n if (s[i] == 'L')\n result += 50;\n if (s[i] == 'I') {\n if (s[i + 1] == 'X') {\n result += 9;\n i += 2;\n } else if (s[i + 1] == 'V') {\n result += 4;\n i += 2;\n } else\n result += 1;\n }\n if (s[i] == 'V')\n result += 5;\n i++;\n }\n return result;\n }\n};", "memory": "10400" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int char_to_val(char c) {\n switch(c) {\n case 'I':\n return 1;\n case 'V':\n return 5;\n case 'X':\n return 10;\n case 'L':\n return 50;\n case 'C':\n return 100;\n case 'D':\n return 500;\n case 'M':\n return 1000;\n }\n\n return -1;\n }\n\n\n int romanToInt(string s) {\n int n = s.size();\n\n int curr = char_to_val(s[n-1]);\n int count = 0;\n \n int res = 0;\n\n // when small number comes before bigger -----> bigger - smaller\n int i = n-2;\n bool flag = true;\n\n while(i >= 0) {\n int next_val = char_to_val(s[i+1]);\n int curr_val = char_to_val(s[i]);\n\n if(next_val > curr_val) {\n int curr = next_val - curr_val;\n res += curr;\n i -= 2;\n }\n else {\n res += next_val;\n i--;\n }\n }\n\n if(i == -1)\n res += char_to_val(s[0]);\n\n cout << flag << endl;\n return res;\n }\n};", "memory": "10500" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int result = 0;\n for (int i = 0; i < s.length(); ++i) {\n char curr_ch = s[i];\n char next_ch = s[i + 1];\n\n switch(s[i]) {\n case 'I': //! IV (4), IX(9)\n if (next_ch == 'V') {\n result += 4;\n ++i;\n } else if (next_ch == 'X') {\n result += 9;\n ++i;\n } else\n result += 1;\n break;\n\n case 'V': result += 5; break;\n case 'X': //! XL(40), XC(90)\n if (next_ch == 'L') {\n result += 40;\n ++i;\n } else if(next_ch == 'C') {\n result += 90;\n ++i;\n } else\n result += 10;\n break;\n\n case 'L': result += 50; break;\n case 'C': //! CD(400), CM(900)\n if (next_ch == 'D') {\n result += 400;\n ++i;\n } else if(next_ch == 'M') {\n result += 900;\n ++i;\n } else\n result += 100;\n break;\n case 'D': result += 500; break;\n case 'M': result += 1000; break;\n }\n }\n return (result);\n }\n};", "memory": "10500" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int ans=0;\n int n=s.size();\n for(int i=0;i<n;i++){\n if (s[i] == 'I') {\n if (i + 1 < n && (s[i + 1] == 'V' || s[i + 1] == 'X')) {\n ans -= 1; \n } else {\n ans += 1;\n }\n }\n if (s[i] == 'V') {\n ans += 5;\n }\n if (s[i] == 'X') {\n if (i + 1 < n && (s[i + 1] == 'L' || s[i + 1] == 'C')) {\n ans -= 10; \n } else {\n ans += 10;\n }\n }\n if (s[i] == 'L') {\n ans += 50;\n }\n if (s[i] == 'C') {\n if (i + 1 < n && (s[i + 1] == 'D' || s[i + 1] == 'M')) {\n ans -= 100;\n } else {\n ans += 100;\n }\n }\n if (s[i] == 'D') {\n ans += 500;\n }\n if (s[i] == 'M') {\n ans += 1000;\n }\n }\n \n return ans;\n }\n};", "memory": "10600" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int x, i, j, suma=0;\n for(i = 0; i < s.length(); i++)\n {\n x=0;\n j = i + 1;\n switch (s[i]){\n case 'I':\n x = 1;\n break;\n case 'V':\n x = 5;\n break;\n case 'X':\n x = 10;\n break;\n case 'L':\n x = 50;\n break;\n case 'C':\n x = 100;\n break;\n case 'D':\n x = 500;\n break;\n case 'M':\n x = 1000;\n break;\n }\n if(s[i] == 'I')\n {\n if(s[j] == 'V')\n {x = 4;\n i++;\n }\n if(s[j] == 'X')\n {x = 9;\n i++;\n } \n }\n if(s[i] == 'X')\n {\n if(s[j] == 'L')\n {x = 40;\n i++;\n }\n if(s[j] == 'C')\n {x = 90;\n i++;\n } \n }\n if(s[i] == 'C')\n {\n if(s[j] == 'D')\n {x = 400;\n i++;\n }\n if(s[j] == 'M')\n {x = 900;\n i++;\n } \n }\n suma = suma+x;\n }\n return suma;\n }\n};", "memory": "10600" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int res = 0;\n int val = 0;\n int val_next = 0;\n\n for (int i = 0; i < s.size(); i++) {\n char ch = s[i];\n \n val = Rom_to_val(ch);\n \n //cout << val << \" \";\n\n if (i+1 < s.size() && val >= Rom_to_val(s[i+1]))\n res += val; \n else if (i+1 < s.size() && val < Rom_to_val(s[i+1]))\n res += val*(-1);\n //cout << res << \" \";\n }\n\n res += val;\n\n return res;\n }\n\nprivate:\n int Rom_to_val(char ch) {\n int val;\n switch(ch){\n case 'I': val = 1; break;\n case 'V': val = 5; break;\n case 'X': val = 10; break;\n case 'L': val = 50; break;\n case 'C': val = 100; break;\n case 'D': val = 500; break;\n case 'M': val = 1000; break;\n }\n return val;\n }\n};", "memory": "10700" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int getVal(char ch) {\n switch(ch) {\n case 'M':\n return 1000;\n case 'D' :\n return 500;\n case 'C' :\n return 100;\n case 'L' :\n return 50;\n case 'X' :\n return 10;\n case 'V' :\n return 5;\n case 'I' :\n return 1;\n default :\n return 1;\n }\n }\n\n int romanToInt(string s) {\n string rev = s;\n reverse(rev.begin(), rev.end());\n int prev = 0;\n int sum = 0;\n\n for(char ch:rev) {\n int curr = getVal(ch);\n\n if (curr < prev) {\n sum -= curr;\n }\n else {\n sum += curr;\n }\n prev = curr;\n }\n\n return sum;\n }\n};", "memory": "10700" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int total = 0;\n int current, previus = 1000;\n\n for(char& c : s){\n current = charToInt(c);\n total += current;\n if (previus < current){\n total -= previus * 2;\n }\n previus = current;\n }\n return total;\n }\n\n int charToInt(char& c){\n switch(c){\n case 'I': return 1;\n case 'V': return 5;\n case 'X': return 10;\n case 'L': return 50;\n case 'C': return 100;\n case 'D': return 500;\n case 'M': return 1000;\n default: return 0;\n }\n }\n\n};", "memory": "10800" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int last = 100000, ans = 0;\n for ( char c: s ) {\n int curr = 1;\n switch (c){\n case 'M':\n curr = 1000;\n break;\n case 'D':\n curr = 500;\n break;\n case 'C':\n curr = 100;\n break;\n case 'L':\n curr = 50;\n break;\n case 'X':\n curr = 10;\n break;\n case 'V':\n curr = 5;\n break;\n }\n if (last < curr) {\n ans -= last + last;\n }\n ans += curr;\n last = curr;\n } \n return ans;\n }\n};", "memory": "10800" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\nint val(char s){\n if(s=='I')\n return 1;\n \n else if(s=='V')\n return 5;\n else if(s=='X')\n return 10;\n else if(s=='L')\n return 50;\n else if(s=='C')\n return 100;\n else if(s=='D')\n return 500;\n else if(s=='M')\n return 1000;\n else\n return 0;\n\n}\n int romanToInt(string s) {\n int n=s.size();\n int sum=val(s[n-1]);\n for(int i=n-2;i>=0;i--){\n if(val(s[i])>=val(s[i+1]))\n sum=sum+val(s[i]);\n else\n sum=sum-val(s[i]);\n }\n return sum;\n }\n};", "memory": "10900" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution \n{\npublic:\n int romanToInt(string s) \n {\n int retInt = 0;\n\n for (int i = 0; i < s.size(); i++)\n {\n switch (s[i])\n {\n case 'I':\n if (s[i + 1] == 'V')\n {\n retInt += RN_IV;\n i++;\n }\n else if (s[i+1] == 'X')\n {\n retInt += RN_IX;\n i++;\n }\n else\n {\n retInt += RN_I;\n }\n break;\n case 'V':\n retInt += RN_V;\n break;\n case 'X':\n if (s[i+1] == 'L')\n {\n retInt += RN_XL;\n i++;\n }\n else if (s[i+1] == 'C')\n {\n retInt += RN_XC;\n i++;\n }\n else\n {\n retInt += RN_X;\n }\n break;\n case 'L':\n retInt += RN_L;\n break;\n case 'C':\n if (s[i+1] == 'D')\n {\n retInt += RN_CD;\n i++;\n }\n else if (s[i+1] == 'M')\n {\n retInt += RN_CM;\n i++;\n }\n else\n {\n retInt += RN_C;\n }\n break;\n case 'D':\n retInt += RN_D;\n break;\n case 'M':\n retInt += RN_M;\n default:\n std::cout << \"Invalid Roman Numberal\" << std::endl;\n break;\n }\n }\n\n return retInt;\n }\nprivate:\n enum romNums\n {\n RN_I = 1,\n RN_IV = 4,\n RN_V = 5,\n RN_IX = 9,\n RN_X = 10,\n RN_XL = 40,\n RN_L = 50,\n RN_XC = 90,\n RN_C = 100,\n RN_CD = 400,\n RN_D = 500,\n RN_CM = 900,\n RN_M = 1000\n };\n};", "memory": "10900" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int size = s.size();\n int temp = 0;\n for(int i = 0; i < size; i++){\n if(s[i] == 'M') temp += 1000;\n else if(s[i] == 'D') temp += 500;\n else if(s[i] == 'L') temp += 50;\n else if(s[i] == 'V') temp += 5;\n else if(s[i] == 'C') {\n if(i+1 == size) temp += 100;\n else{\n if(s[i+1] == 'M') {\n temp += 900;\n i += 1;\n }\n else if(s[i+1] == 'D') {\n temp += 400;\n i += 1;\n }\n else{\n temp += 100;\n }\n }\n }\n else if(s[i] == 'X') {\n if(i+1 == size) temp += 10;\n else{\n if(s[i+1] == 'C') {\n temp += 90;\n i += 1;\n }\n else if(s[i+1] == 'L') {\n temp += 40;\n i += 1;\n }\n else{\n temp += 10;\n }\n }\n }\n else if(s[i] == 'I') {\n if(i+1 == size) temp += 1;\n else{\n if(s[i+1] == 'X') {\n temp += 9;\n i += 1;\n }\n else if(s[i+1] == 'V') {\n temp += 4;\n i += 1;\n }\n else{\n temp += 1;\n }\n }\n }\n }\n return temp;\n }\n};", "memory": "11000" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n vector<char> chars(s.begin(), s.end());\n\n bool seenI = false;\n bool seenX = false;\n bool seenC = false;\n\n int val = 0;\n\n for (const char &a : chars) {\n if (a == 'I') {\n seenI = true;\n val += 1;\n }\n if (a == 'V') {\n if (seenI) val += 3;\n else val += 5;\n seenI = false;\n seenX = false;\n seenC = false;\n }\n if (a == 'X') {\n if (seenI) val += 8;\n else val += 10;\n seenI = false;\n seenX = true;\n seenC = false;\n }\n if (a == 'L') {\n if (seenX) val += 30;\n else val += 50;\n seenI = false;\n seenX = false;\n seenC = false;\n }\n if (a == 'C') {\n if (seenX) val += 80;\n else val += 100;\n seenI = false;\n seenX = false;\n seenC = true;\n }\n if (a == 'D') {\n if (seenC) val += 300;\n else val += 500;\n seenI = false;\n seenX = false;\n seenC = false;\n }\n if (a == 'M') {\n if (seenC) val += 800;\n else val += 1000;\n seenI = false;\n seenX = false;\n seenC = false;\n }\n }\n return val;\n }\n};", "memory": "11000" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n vector<char> arr(s.length());\n int n = s.length();\n for(int i=0;i<n;i++){\n arr[i] = s[i];\n }\n int sum = 0;\n for(int i=0;i<n;i++){\n if(i != (n-1)){\n if(arr[i] == 'I' && arr[i+1] == 'V'){\n sum += 4;\n i++;\n continue;\n }\n if(arr[i] == 'I' && arr[i+1] == 'X'){\n sum += 9;\n i++;\n continue;\n }\n if(arr[i] == 'X' && arr[i+1] == 'L'){\n sum += 40;\n i++;\n continue;\n } else if(arr[i] == 'X' && arr[i+1] == 'C'){\n sum += 90;\n i++;\n continue;\n }\n if(arr[i] == 'C' && arr[i+1] == 'D'){\n sum += 400;\n i++;\n continue;\n } else if(arr[i] == 'C' && arr[i+1] == 'M'){\n sum += 900;\n i++;\n continue;\n }\n }\n if(arr[i] == 'I'){\n sum += 1;\n } else if(arr[i] == 'V'){\n sum += 5;\n } else if(arr[i] == 'X'){\n sum += 10;\n } if(arr[i] == 'L'){\n sum += 50;\n } else if(arr[i] == 'C'){\n sum += 100;\n } else if(arr[i] == 'D'){\n sum += 500;\n } else if(arr[i] == 'M'){\n sum += 1000;\n }\n }\n return sum;\n }\n};", "memory": "11100" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int n = s.length();\n vector<char> romans = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n vector<int> numbers = {1, 5, 10, 50, 100, 500, 1000};\n\n int result = 0;\n int temp = 0;\n\n for(int i = 0; i < romans.size(); i++)\n {\n if(romans[i] != s[n - 1])\n temp++;\n else\n break;\n }\n\n result += numbers[temp];\n\n for(int i = n - 2; i >= 0; i--)\n {\n if((find(romans.begin(), romans.end(), s[i]) - romans.begin()) > temp)\n {\n temp = find(romans.begin(), romans.end(), s[i]) - romans.begin();\n result += numbers[temp];\n }\n else if((find(romans.begin(), romans.end(), s[i]) - romans.begin()) == temp)\n {\n result += numbers[temp];\n }\n else\n {\n result -= numbers[find(romans.begin(), romans.end(), s[i]) - romans.begin()];\n }\n }\n\n return result;\n }\n};", "memory": "11200" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int n = s.length();\n vector<char> romans = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n vector<int> numbers = {1, 5, 10, 50, 100, 500, 1000};\n\n int result = 0;\n int temp = 0;\n\n for(int i = 0; i < romans.size(); i++)\n {\n if(romans[i] != s[n - 1])\n temp++;\n else\n break;\n }\n\n result += numbers[temp];\n\n for(int i = n - 2; i >= 0; i--)\n {\n int currInd = find(romans.begin(), romans.end(), s[i]) - romans.begin();\n if(currInd > temp)\n {\n temp = currInd;\n result += numbers[temp];\n }\n else if(currInd == temp)\n {\n result += numbers[temp];\n }\n else\n {\n result -= numbers[currInd];\n }\n }\n\n return result;\n }\n};", "memory": "11300" }
13
<p>Roman numerals are represented by seven different symbols:&nbsp;<code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p> <pre> <strong>Symbol</strong> <strong>Value</strong> I 1 V 5 X 10 L 50 C 100 D 500 M 1000</pre> <p>For example,&nbsp;<code>2</code> is written as <code>II</code>&nbsp;in Roman numeral, just two ones added together. <code>12</code> is written as&nbsp;<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> <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> <ul> <li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9.&nbsp;</li> <li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90.&nbsp;</li> <li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li> </ul> <p>Given a roman numeral, convert it to an integer.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;III&quot; <strong>Output:</strong> 3 <strong>Explanation:</strong> III = 3. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;LVIII&quot; <strong>Output:</strong> 58 <strong>Explanation:</strong> L = 50, V= 5, III = 3. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;MCMXCIV&quot; <strong>Output:</strong> 1994 <strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 15</code></li> <li><code>s</code> contains only&nbsp;the characters <code>(&#39;I&#39;, &#39;V&#39;, &#39;X&#39;, &#39;L&#39;, &#39;C&#39;, &#39;D&#39;, &#39;M&#39;)</code>.</li> <li>It is <strong>guaranteed</strong>&nbsp;that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int romanToInt(string s) {\n int n = s.size();\n\n vector<int> num(26, 0);\n num['I'-'A'] = 1;\n num['V'-'A'] = 5;\n num['X'-'A'] = 10;\n num['L'-'A'] = 50;\n num['C'-'A'] = 100;\n num['D'-'A'] = 500;\n num['M'-'A'] = 1000;\n\n int result = num[s[n-1]-'A'];\n for(int i=n-2;i>=0;i--){\n if(s[i] == 'I'){\n if(s[i+1] == 'V' || s[i+1] == 'X'){\n result -= 1;\n }else{\n result += 1;\n }\n }else if(s[i] == 'X'){\n if(s[i+1] == 'L' || s[i+1] == 'C'){\n result -= 10;\n }else{\n result += 10;\n }\n }else if(s[i] == 'C'){\n if(s[i+1] == 'D' || s[i+1] == 'M'){\n result -= 100;\n }else{\n result += 100;\n }\n }else{\n result += num[s[i]-'A'];\n }\n }\n return result;\n }\n};", "memory": "11400" }