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) {
if(num < 1 || num > 3999) return null;
String[] roman = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int[] numbers = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
StringBuffer result = new StringBuffer();
for(int i = 0; num > 0; i++){
while(num >= numbers[i]){
result.append(roman[i]);
num -= numbers[i];
}
}
return result.toString();
}
}