Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
Examples at: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm
We can start from subtract base=1000 from number, if it is greater than base, we append the corresponding character to the result.
However, we need to deal with two cases.
So we can deal with the problem using the following logic: (suppose roman(base) means convert the number to roman number)
For example, number = 45
Time: In the worst case, each base will be used three times in the loop. However, it is impossible to reach the worst case, since 4500 will never be represented by "MMMDDDD", actually it exceeds the maximum value 3999. So time complexity is smaller than O(7*3)
Space: O(1), only two small arrays
public class Solution {
private static final char[] ROMANS = {'M', 'D', 'C', 'L', 'X', 'V', 'I'};
private static final int[] BASES = {1000, 500, 100, 50, 10, 5, 1};
public String intToRoman(int n) {
assert n >= 1 && n <= 3999;
StringBuilder sb = new StringBuilder();
int baseIndex = 0;
while (n != 0) {
if (baseIndex % 2 == 0 && n / BASES[baseIndex] == 4) {
sb.append(ROMANS[baseIndex]);
sb.append(ROMANS[baseIndex - 1]);
n -= 4 * BASES[baseIndex];
} else if (n >= BASES[baseIndex]) {
sb.append(ROMANS[baseIndex]);
n -= BASES[baseIndex];
} else if (baseIndex % 2 == 0 && n / BASES[baseIndex + 2] == 9) {
sb.append(ROMANS[baseIndex + 2]);
sb.append(ROMANS[baseIndex]);
n -= 9 * BASES[baseIndex + 2];
} else {
baseIndex++;
}
}
return sb.toString();
}
}
Get represensation of all possible combination on each digit.
Time: O(1)
Space: O(1)
public class Solution {
private static final String M[] = {"", "M", "MM", "MMM"};
private static final String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
private static final String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
private static final String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
public String intToRoman(int n) {
assert n >= 1 && n <= 3999;
return M[n / 1000] + C[(n % 1000) / 100] + X[(n % 100) / 10] + I[n % 10];
}
}