0ms solution with C, a newbie's solution


  • 0
    S

    11 lines code, easy to understand.

    char* intToRoman(int num) {
    	const char *rom1[4] = { "", "M", "MM", "MMM"};
    	const char *rom2[10] = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
    	const char *rom3[10] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
    	const char *rom4[10] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
    	char  *rom = calloc(16 ,sizeof(char));
    	strcat(rom, rom1[num / 1000]);
    	strcat(rom, rom2[(num % 1000) / 100]);
    	strcat(rom, rom3[(num % 100) / 10]);
    	strcat(rom, rom4[num % 10]);
    	return rom;
    }
    

Log in to reply
 

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