Two lines can do the job


  • 50
    B

    String[] romanPieces={"","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","MMMM"};
    return romanPieces[num/1000+30]+romanPieces[(num/100)%10+20]
    +romanPieces[(num/10)%10+10]+romanPieces[num%10];


  • 1
    Y

    C++ version

    public:
        string intToRoman(int num) {
            string romanPieces[]={"","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","MMMM"};
            return romanPieces[num/1000+30]+romanPieces[(num/100)%10+20]
                +romanPieces[(num/10)%10+10]+romanPieces[num%10];
        }
    };

Log in to reply
 

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