难度:中等
解题思路:和我之前做的罗马转整数一样,暴力就完事了,个人感觉难度应该是简单,上代码
class Solution {
public String intToRoman(int num) {
HashMap<Integer,String> map = new HashMap<>();
map.put(1,"I");
map.put(4,"IV");
map.put(5,"V");
map.put(9,"IX");
map.put(10,"X");
map.put(40,"XL");
map.put(50,"L");
map.put(90,"XC");
map.put(100,"C");
map.put(400,"CD");
map.put(500,"D");
map.put(900,"CM");
map.put(1000,"M");
String result = "";
while (num!=0){
if (num >=1000){
num = num-1000;
result += map.get(1000);
}else if (num >= 900){
num = num-900;
result += map.get(900);
}else if (num >= 500){
num = num-500;
result += map.get(500);
}else if (num >= 400){
num = num-400;
result += map.get(400);
}else if (num >= 100){
num = num-100;
result += map.get(100);
}else if (num >= 90){
num = num-90;
result += map.get(90);
}else if (num >= 50){
num = num-50;
result += map.get(50);
}else if (num >= 40){
num = num-40;
result += map.get(40);
}else if (num >= 10){
num = num-10;
result += map.get(10);
}else if (num >= 9){
num = num-9;
result += map.get(9);
}else if (num >= 5){
num = num-5;
result += map.get(5);
}else if (num >= 4){
num = num-4;
result += map.get(4);
}else if (num >= 1){
num = num-1;
result += map.get(1);
}
}
return result;
}
}
|