Skip to content

Commit 3ef46c0

Browse files
authored
Update Solution.c
1 parent f2beb68 commit 3ef46c0

File tree

1 file changed

+14
-11
lines changed
  • solution/0000-0099/0012.Integer to Roman

1 file changed

+14
-11
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
char* intToRoman(int num) {
2-
static char res[20];
3-
res[0] = '\0';
1+
static const char* cs[] = {
2+
"M", "CM", "D", "CD", "C", "XC",
3+
"L", "XL", "X", "IX", "V", "IV", "I"};
44

5-
int vals[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
6-
char* syms[] = {"M", "CM", "D", "CD", "C", "XC", "L",
7-
"XL", "X", "IX", "V", "IV", "I"};
5+
static const int vs[] = {
6+
1000, 900, 500, 400, 100, 90,
7+
50, 40, 10, 9, 5, 4, 1};
88

9-
for (int i = 0; i < 13; i++) {
10-
while (num >= vals[i]) {
11-
strcat(res, syms[i]);
12-
num -= vals[i];
9+
char* intToRoman(int num) {
10+
static char ans[20];
11+
ans[0] = '\0';
12+
for (int i = 0; i < 13; ++i) {
13+
while (num >= vs[i]) {
14+
num -= vs[i];
15+
strcat(ans, cs[i]);
1316
}
1417
}
15-
return res;
18+
return ans;
1619
}

0 commit comments

Comments
 (0)