Simple Solution


  • 465
    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];
    }
    

  • 4

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


  • 0
    A
    This post is deleted!

  • 0
    T

    Great solution!


  • 1
    S

    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


  • 7
    V

    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];
            }
        };

  • 5
    L

    I'm sorry but this is too beautiful


  • 2
    A

    Nice solution


  • 0
    L

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


  • 35
    G

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


  • 1
    H

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


  • 0
    K

    SUPERB SOLUTION


  • 0
    C

    Soooo beautiful


  • 0
    R

    beautiful man so clear ... never imagined that.


  • 0
    H

    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];
    }

  • 0
    S

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


  • 0
    C

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


  • 0
    H

    Hahahaso smart


  • 0
    S

    Nice solution!!!!


  • 1
    E

    @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.


Log in to reply
 

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