# Middle-题目10：12. Integer to Roman

72人阅读 评论(0)

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 <= 9) {
switch (num) {
case 1:{return "I";}
case 2:case 3:{return "I"+intToRoman(num-1);}
case 4:case 9:{return "I"+intToRoman(num+1);}
case 5:{return "V";}
default:{return "V"+intToRoman(num-5);}//6,7,8
}
}
else if (num >= 10 && num <= 99) {
switch (num) {
case 10:{return "X";}
case 50:{return "L";}
case 40:case 90:{return "X"+intToRoman(num+10);}
default: {
if(num > 90)
return "X"+intToRoman(num+10);
else if(num > 50)
return "L"+intToRoman(num-50);
else if(num > 40 && num < 50)  //XL
return "X"+intToRoman(num+10);
else
return "X"+intToRoman(num-10);
}
}
}
else if (num >= 100 && num <= 999) {
switch (num) {
case 100:{return "C";}
case 500:{return "D";}
case 400:case 900:{return "C"+intToRoman(num+100);}
default:{
if(num > 900)
return "C"+intToRoman(num+100);
else if(num > 500)
return "D"+intToRoman(num-500);
else if(num > 400 && num < 500)
return "C"+intToRoman(num+100);
else
return "C"+intToRoman(num-100);
}
}
}
else { //1000~3999
switch (num) {
case 1000:{return "M";}
default:{return "M"+intToRoman(num-1000);}
}
}
}
}

11ms，beats24.85%，众数8ms，23.08%
Cmershen的碎碎念：

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：56303次
• 积分：3204
• 等级：
• 排名：第10668名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：7条
文章分类
最新评论