Implement the function that converts an integer to its Roman numeral representation.
Seven different symbols represent Roman numerals with the following values:
Example-1:
Input: num = 3749
Output: "MMMDCCXLIX"
Explanation:
Note: 49 is not 1 (I) less of 50 (L) because the conversion is based on decimal places.
Example-2:
Input: num = 58
Output: "LVIII"
Explanation:
Example-3:
Input: num = 1994
Output: "MCMXCIV"
Explanation:
Problem Statement:
Approach:
Time & Space Complexity Analysis:
Java Implementation:
public String intToRoman(int num) {
StringBuilder res = new StringBuilder();
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] symbols = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
for(int i = 0; i < values.length; i++) {
while (num >= values[i]) {
res.append(symbols[i]);
num = num - values[i];
}
}
return res.toString();
}