Algorithm


Problem Name:  12. Integer to Roman

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. 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. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

Example 1:

Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.

Example 2:

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 3:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

  • 1 <= num <= 3999

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


typedef struct {
 	char *s;
 	int v;
} map_t;

const map_t map[] = {
 	{ "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 }
};

#define SZ  (sizeof(map)/sizeof(map[0]))

char* intToRoman(int num) {
	int n, l = SZ - 1;

	char *p = malloc(1024);
    //assert(p);
	p[0] = 0;
	
	while (num) {
		n = num / map[l].v;
		while (n) {
			strcat(p, map[l].s);
			n --;
		}
 	num = num % map[l].v;
 	l --;
	}
 	
 	return p;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 3

Output

x
+
cmd
"III"

#2 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    string intToRoman(int num) {
        string M[] = {"", "M", "MM", "MMM"};
        string C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        string X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
        string I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
        return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 58

Output

x
+
cmd
"LVIII"

#3 Code Example with Java Programming

Code - Java Programming


class Solution {
  
  private static final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  private static final String[] symbols = {
    "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  
  public String intToRoman(int num) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i  <  values.length && num > 0; i++) {
      int factor = num / values[i];
      num %= values[i];
      if (factor > 0) {
        for (int j = 1; j  < = factor; j++) {
          sb.append(symbols[i]);
        }
      }
    }
    return sb.toString();
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 1994

Output

x
+
cmd
"MCMXCIV"

#4 Code Example with Javascript Programming

Code - Javascript Programming


const map = {
  "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"
};
const intToRoman = function(number) {
  const l = fkey(map, number);
  if (number == +l) {
    return map[number];
  }
  return map[l] + intToRoman(number - +l);
};

function fkey(m, num) {
  const keys = Object.keys(m);
  const sArr = keys.filter(el => +el  < = num);
  return +Math.max.apply(Math, sArr);
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 3

Output

x
+
cmd
"III"

#5 Code Example with Python Programming

Code - Python Programming


class Solution:
    def intToRoman(self, num):
        s = "M" * (num // 1000)
        s += "CM" if num % 1000 >= 900 else "D" *((num % 1000) // 500)
        s += "CD" if num % 500 >= 400 and s[-2:] != "CM" else "C" * ((num % 500) // 100)  if num % 500 < 400 else ""
        s += "XC" if num % 100 >= 90 else "L" * ((num % 100) // 50)
        s += "XL" if num % 50 >= 40 and s[-2:] != "XC" else "X" * ((num % 50) // 10)  if num % 50 < 40 else ""
        s += "IX" if num % 10 >= 9 else "V" * ((num % 10) // 5)
        s += "IV" if num % 5 >= 4 and s[-2:] != "IX" else "I" * ((num % 5) // 1) if num % 5 < 4 else ""
        return s
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 58

Output

x
+
cmd
"LVIII"

#6 Code Example with C# Programming

Code - C# Programming


using System.Text;

namespace LeetCode
{
    public class _012_IntegerToRoman
    {
        public string IntToRoman(int num)
        {
            string[] symbol = { "MMM", "MM", "M", "CM", "DCCC", "DCC", "DC", "D", "CD", "CCC", "CC", "C", "XC", "LXXX", "LXX", "LX", "L", "XL", "XXX", "XX", "X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I" };
            int[] value = { 3000, 2000, 1000, 900, 800, 700, 600, 500, 400, 300, 200, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };

            var result = new StringBuilder();
            var index = 0;
            while (num != 0)
            {
                if (num >= value[index])
                {
                    num -= value[index];
                    result.Append(symbol[index]);
                }
                else
                {
                    index++;
                }
            }

            return result.ToString();
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
num = 3

Output

x
+
cmd
"III"
Advertisements

Demonstration


Previous
#11 Leetcode Container With Most Water Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#13 Leetcode Roman to Integer Solution in C, C++, Java, JavaScript, Python, C# Leetcode