12. Leetc – Roman

Time:2022-5-19

Method 1: greedy algorithm

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * intToRoman(int num) {
  struct intToRoman
  {
    int num;
    char *str;
  } list[13] = {
      {1000, "M"},
      {900, "CM"},
      {500, "D"},
      {400, "CD"},
      {100, "C"},
      {90, "XC"},
      {50, "L"},
      {40, "XL"},
      {10, "X"},
      {9, "IX"},
      {5, "V"},
      {4, "IV"},
      {1, "I"},
  };
  int count[13] = {0};
  char *str = (char*)malloc(17 * sizeof(char));
  memset(str, '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * intToRoman(int num) {
struct intToRoman
{
int num;
char *str;
} list[13] = {
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"},
};
int count[13] = {0};
char *str = (char*)malloc(17 * sizeof(char));
memset(str, '\0', 17);
for (int i = 0; i < 13; i++) {
int count = num / list[i].num;
while (count-- > 0)
{
strcat(str, list[i].str);
}
num %= list[i].num;
}
strcat(str, '\0');
return str;
}
int main(void) {
int num = 3999;
printf("%s\n", intToRoman(num));
}
', 17); for (int i = 0; i < 13; i++) { int count = num / list[i].num; while (count-- > 0) { strcat(str, list[i].str); } num %= list[i].num; } strcat(str, '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * intToRoman(int num) {
struct intToRoman
{
int num;
char *str;
} list[13] = {
{1000, "M"},
{900, "CM"},
{500, "D"},
{400, "CD"},
{100, "C"},
{90, "XC"},
{50, "L"},
{40, "XL"},
{10, "X"},
{9, "IX"},
{5, "V"},
{4, "IV"},
{1, "I"},
};
int count[13] = {0};
char *str = (char*)malloc(17 * sizeof(char));
memset(str, '\0', 17);
for (int i = 0; i < 13; i++) {
int count = num / list[i].num;
while (count-- > 0)
{
strcat(str, list[i].str);
}
num %= list[i].num;
}
strcat(str, '\0');
return str;
}
int main(void) {
int num = 3999;
printf("%s\n", intToRoman(num));
}
'); return str; } int main(void) { int num = 3999; printf("%s\n", intToRoman(num)); }

perhaps

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * intToRoman(int num) {
  int numArr[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
  char *roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
  int count[13] = {0};
  char *str = (char*)malloc(17 * sizeof(char));
  memset(str, '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * intToRoman(int num) {
int numArr[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
char *roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int count[13] = {0};
char *str = (char*)malloc(17 * sizeof(char));
memset(str, '\0', 17);
for (int i = 0; i < 13; i++) {
int count = num / numArr[i];
while (count-- > 0)
{
strcat(str, roman[i]);
}
num %= numArr[i];
}
return str;
}
int main(void) {
int num = 3999;
printf("%s\n", intToRoman(num));
}
', 17); for (int i = 0; i < 13; i++) { int count = num / numArr[i]; while (count-- > 0) { strcat(str, roman[i]); } num %= numArr[i]; } return str; } int main(void) { int num = 3999; printf("%s\n", intToRoman(num)); }

12. Leetc - Roman

Time complexity: O (1). Because the number of cycles has a constant upper limit, the time complexity is in the order of constant
Space complexity: O (1). The amount of memory used will not change with the size of the input integer, so it is also constant level

Method 2. Hard code the Roman numerals that may appear in thousands, hundreds, tens and individuals, then divide and remainder the integer to obtain the integer digits of thousands, hundreds, tens and individuals, and then find the corresponding Roman numerals and splice them.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *intToRoman(int num)
{
  char *thousandsRoman[] = {"", "M", "MM", "MMM"};
  char *hundredsRoman[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
  char *tensRoman[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
  char *onesRoman[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
  char *str = (char *)malloc(16 * sizeof(char));
  memset(str, '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *intToRoman(int num)
{
char *thousandsRoman[] = {"", "M", "MM", "MMM"};
char *hundredsRoman[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
char *tensRoman[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
char *onesRoman[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
char *str = (char *)malloc(16 * sizeof(char));
memset(str, '\0', 16);
int tods = num / 1000;
int huds = (num % 1000) / 100;
int tens = (num % 100) / 10;
int ones = num % 10;
strcat(str, thousandsRoman[tods]);
strcat(str, hundredsRoman[huds]);
strcat(str, tensRoman[tens]);
strcat(str, onesRoman[ones]);
return str;
}
int main(void)
{
int num = 3999;
printf("%s\n", intToRoman(num));
}
', 16); int tods = num / 1000; int huds = (num % 1000) / 100; int tens = (num % 100) / 10; int ones = num % 10; strcat(str, thousandsRoman[tods]); strcat(str, hundredsRoman[huds]); strcat(str, tensRoman[tens]); strcat(str, onesRoman[ones]); return str; } int main(void) { int num = 3999; printf("%s\n", intToRoman(num)); }

12. Leetc - Roman

The time complexity is O (1). The above codes are constant level operations. Even string splicing is short string splicing, and the time consumption is also constant level, so it is O (1)
The spatial complexity is O (1). The spatial complexity is clear at a glance. It is at the constant level and does not change with the size of the input number. Therefore, it is also o (1)

The comparison between method 1 and method 2 is that if you want to expand Roman numerals, with the increase of the input number scale, method 2 will need more and more numbers to be hard coded, while method 1 needs to add fewer special Roman numerals, which is more convenient to modify.