Copy class Solution {
public :
string intToRoman ( int num) {
string res;
for ( int i = 0 ; i < 4 ; i ++ ) {
int digit = num % 10 ;
num /= 10 ;
if (digit == 0 ) continue ;
if (i == 0 ) {
if (digit <= 3 ) {
for ( int j = 0 ; j < digit; j ++ )
res += "I" ;
}
else if (digit == 4 ) res += "IV" ;
else if (digit == 5 ) res += "V" ;
else if ( 5 < digit && digit <= 8 ) {
res += "V" ;
for ( int j = 0 ; j < digit - 5 ; j ++ )
res += "I" ;
}
else if (digit == 9 ) res += "IX" ;
}
else if (i == 1 ) {
if (digit <= 3 ) {
for ( int j = 0 ; j < digit; j ++ )
res . insert ( 0 , "X" );
}
else if (digit == 4 ) res . insert ( 0 , "XL" );
else if (digit == 5 ) res . insert ( 0 , "L" );
else if ( 5 < digit && digit <= 8 ) {
res . insert ( 0 , "L" );
for ( int j = 0 ; j < digit - 5 ; j ++ )
res . insert (j + 1 , "X" );
}
else if (digit == 9 ) res . insert ( 0 , "XC" );
}
else if (i == 2 ) {
if (digit <= 3 ) {
for ( int j = 0 ; j < digit; j ++ )
res . insert ( 0 , "C" );
}
else if (digit == 4 ) res . insert ( 0 , "CD" );
else if (digit == 5 ) res . insert ( 0 , "D" );
else if ( 5 < digit && digit <= 8 ) {
res . insert ( 0 , "D" );
for ( int j = 0 ; j < digit - 5 ; j ++ )
res . insert (j + 1 , "C" );
}
else if (digit == 9 ) res . insert ( 0 , "CM" );
}
else if (i == 3 ) {
for ( int j = 0 ; j < digit; j ++ )
res . insert ( 0 , "M" );
}
}
return res;
}
};
Runtime: 8 ms, faster than 78.84% of C++ online submissions for Integer to Roman. Memory Usage: 8.5 MB, less than 67.74% of C++ online submissions for Integer to Roman.
Copy class Solution {
public :
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 ];
}
};