给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
示例 1:
输入: 3
输出: "III"
示例 2:
输入: 4
输出: "IV"
示例 3:
输入: 9
输出: "IX"
示例 4:
输入: 58
输出: "LVIII"
解释: C = 100, L = 50, XXX = 30, III = 3.
示例 5:
输入: 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
class Solution {
public String intToRoman(int num) { int[]values = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
String[]strs = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
StringBuilder sb = new StringBuilder();
for (int i=0;i<values.length;i++) {
while (num >= values[i]) {
num-=values[i];
sb.append(strs[i]);
}
}
return sb.toString(); }
}