[C]Intuitive Solution without fancy skills


  • 0
    H
    char* M[ 4] =  {"", "M", "MM", "MMM"}; /*1000*/
    char* C[10] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; /*100*/
    char* X[10] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}; /*10*/
    char* I[10] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}; /*1, 2, 3, 4, 5, 6, 7...*/
    char* intToRoman(int num) {
        char*str = calloc(100,1);
        memcpy(str, M[num/1000], strlen(M[num/1000]));
        memcpy(str + strlen(M[num/1000]), C[(num%1000)/100], strlen(C[(num%1000)/100]));
        memcpy(str + strlen(M[num/1000]) + strlen(C[(num%1000)/100]), X[(num%100)/10], strlen(X[(num%100)/10]));
        memcpy(str + strlen(M[num/1000]) + strlen(C[(num%1000)/100]) + strlen(X[(num%100)/10]), I[num%10], strlen(I[num%10]));
        return str;
    }
    

Log in to reply
 

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