Integer to Roman

Medium~15 min

Given an integer num in the range [1, 3999], convert it to a Roman numeral string.

Roman numeral symbols and their values:

| Symbol | Value | |--------|-------| | M | 1000 | | CM | 900 | | D | 500 | | CD | 400 | | C | 100 | | XC | 90 | | L | 50 | | XL | 40 | | X | 10 | | IX | 9 | | V | 5 | | IV | 4 | | I | 1 |

Roman numerals are formed by greedily choosing the largest possible symbol at each step.

Examples

Example 1
Input: num = 3749
Output: "MMMDCCXLIX"
Explanation: M=1000, M=1000, M=1000, D=500, CC=200, XL=40, IX=9. Total = 3749.
Example 2
Input: num = 58
Output: "LVIII"
Explanation: L=50, V=5, III=3. Total = 58.
Example 3
Input: num = 1994
Output: "MCMXCIV"
Explanation: M=1000, CM=900, XC=90, IV=4. Total = 1994.

Constraints

  • 1 <= num <= 3999
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run