Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题目大意:给定[1,3999]之间的某个整数,将其转为罗马数字。
解题思路:代码如下:(93ms,beats 75.93%)
public class Solution {
public String intToRoman(int num) {
int[] ints = new int[]{
1000,
900,500,400,100,
90,50,40,10,
9,5,4,1
};
String[] romans = new String[]{
"M",
"CM", "D", "CD", "C",
"XC", "L", "XL", "X",
"IX", "V", "IV", "I"
};
StringBuilder sb = new StringBuilder();
int index = 0;
while(num>0){
if(num-ints[index]>=0){
num-=ints[index];
sb.append(romans[index]);
}
else
index++;
}
return sb.toString();
}
}