Total Accepted: 7867 Total Submissions: 24270
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
public class Solution {
public String intToRoman(int num) {
String[] value = {"I", "IV", "V", "IX", "X", "XL", "L",
"XC", "C", "CD", "D", "CM", "M"};
int[] key = {1, 4, 5, 9, 10, 40, 50, 90, 100,
400, 500, 900, 1000};
StringBuffer str = new StringBuffer();
for (int i = key.length -1; num > 0; ) {
if (num >= key[i]) {
num -= key[i];
str.append(value[i]);
} else {
i--;
}
}
return str.toString();
}
}