- Integer to Roman
Example
Solution
Time Complexity: O(m), space complexity:O(m), where m is the symbol length
class Solution {
public String intToRoman(int num) {
String[] symbol = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};
int[] value = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
String rev = "";
for(int i = value.length-1; num > 0 && i >= 0 ;) {
if(num >= value[i]) {
rev += symbol[i];
num -= value[i];
} else {
i--;
}
}
return rev;
}
}