# Simple Solution

• ``````public static String intToRoman(int num) {
String M[] = {"", "M", "MM", "MMM"};
String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
}
``````

• this is awesome. Simple but very efficient for discrete answer problem like this

• This post is deleted!

• Great solution!

• string intToRoman(int num)
{
string source[34]={ "","I","II","III","IV","V","VI","VII","VIII","IX",
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
"","M","MM","MMM",};
return source[num/1000%10+30]+source[num/100%10+20]+source[num/10%10+10]+source[num%10];
}

We have almost same idear

• It's easy to code after known how to represent roman's numbers,this is my C++ code same likes to yours.

``````class Solution {
public:
string intToRoman(int num) {
static const string s[4][10]=
{
{"","I","II","III","IV","V","VI","VII","VIII","IX"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","M","MM","MMM"},
};

return s[3][num/1000%10] + s[2][num/100%10] + s[1][num/10%10] + s[0][num%10];
}
};``````

• I'm sorry but this is too beautiful

• Nice solution

• Ehm it works for 10, what are you talking about?

• my solution is 100 lines, so when I saw your code, I was ....wocao!!!

• simple and clear. when seeing this I was like "wocao!!!" too....

• SUPERB SOLUTION

• Soooo beautiful

• beautiful man so clear ... never imagined that.

• c++version：

``````string intToRoman(int num) {
vector<string> M = { "", "M", "MM", "MMM" };
vector<string> C = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
vector<string> X = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
vector<string> I = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
}``````

• @gschengcong Your comment let me know why I like to do the Leetcode hahahah

• Pardon me if I am wrong, but isn't largest M value should be MMMM - four M as largest roman number value is 4999

• Hahahaso smart

• Nice solution!!!!

• @carrom
The question says that input is guaranteed to be within the range from 1 to 3999.
Because 4000 in roman numeral is MV ; the 'V' has a overline ,but not MMMM.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.