<\/span><\/h2>\n\n\n\nRoman numerals are represented by seven different symbols: I, V, X, L, C, D<\/code> and M<\/code>.<\/p>\n\n\n\nSymbol Value<\/strong>\r\nI 1\r\nV 5\r\nX 10\r\nL 50\r\nC 100\r\nD 500\r\nM 1000<\/code><\/pre>\n\n\n\nFor example, 2<\/code> is written as II<\/code> in Roman numeral, just two one’s added together. 12<\/code> is written as XII<\/code>, which is simply X + II<\/code>. The number 27<\/code> is written as XXVII<\/code>, which is XX + V + II<\/code>.<\/p>\n\n\n\nRoman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII<\/code>. Instead, the number four is written as 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 IX<\/code>. There are six instances where subtraction is used:<\/p>\n\n\n\nI<\/code> can be placed before V<\/code> (5) and X<\/code> (10) to make 4 and 9.<\/li>X<\/code> can be placed before L<\/code> (50) and C<\/code> (100) to make 40 and 90.<\/li>C<\/code> can be placed before D<\/code> (500) and M<\/code> (1000) to make 400 and 900.<\/li><\/ul>\n\n\n\nGiven an integer, convert it to a roman numeral.<\/p>\n\n\n\n
<\/span>Example 1 :<\/strong><\/span><\/h3>\n\n\n\n\nInput: num = 3\r\nOutput: \"III\"\r\nExplanation: 3 is represented as 3 ones.\n<\/code><\/pre>\n\n\n\n<\/span>Example 2 :<\/strong><\/span><\/h3>\n\n\n\n\nInput: num = 58\r\nOutput: \"LVIII\"\r\nExplanation: L = 50, V = 5, III = 3.\n<\/code><\/pre>\n\n\n\n<\/span>Example 3 :<\/strong><\/span><\/h3>\n\n\n\n\nInput: num = 1994\r\nOutput: \"MCMXCIV\"\r\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.\n<\/code><\/pre>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h3>\n\n\n\n1 <= num <= 3999<\/code><\/li><\/ul>\n\n\n\nNow, let’s see the code of 12. Integer to Roman<\/strong><\/strong> – Leetcode Solution.<\/p>\n\n\n\n<\/span>Integer to Roman<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong> – Leetcode Solution<\/span><\/h1>\n\n\n\n<\/span>12. Integer to Roman<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong> – Solution in Java<\/strong><\/span><\/h3>\n\n\n\nclass Solution {\n public String intToRoman(int num) {\n int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};\n String[] strs = {\"M\",\"CM\",\"D\",\"CD\",\"C\",\"XC\",\"L\",\"XL\",\"X\",\"IX\",\"V\",\"IV\",\"I\"};\n\n StringBuilder sb = new StringBuilder();\n\n for(int i=0;i<values.length;i++) {\n while(num >= values[i]) {\n num -= values[i];\n sb.append(strs[i]);\n }\n }\n return sb.toString();\n }\n}<\/pre>\n\n\n\n<\/span>