Skip to content

Commit a33c203

Browse files
authored
Update README.md
1 parent 3ef46c0 commit a33c203

File tree

1 file changed

+18
-17
lines changed
  • solution/0000-0099/0012.Integer to Roman

1 file changed

+18
-17
lines changed

solution/0000-0099/0012.Integer to Roman/README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,25 +302,26 @@ class Solution {
302302

303303
#### C
304304

305-
```C
306-
307-
char *intToRoman(int num) {
308-
static char res[20];
309-
res[0] = '\0';
310-
311-
int vals[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
312-
char *syms[] = {"M", "CM", "D", "CD", "C", "XC", "L",
313-
"XL", "X", "IX", "V", "IV", "I"};
314-
315-
for (int i = 0; i < 13; i++) {
316-
while (num >= vals[i]) {
317-
strcat(res, syms[i]);
318-
num -= vals[i];
305+
```c
306+
static const char* cs[] = {
307+
"M", "CM", "D", "CD", "C", "XC",
308+
"L", "XL", "X", "IX", "V", "IV", "I"};
309+
310+
static const int vs[] = {
311+
1000, 900, 500, 400, 100, 90,
312+
50, 40, 10, 9, 5, 4, 1};
313+
314+
char* intToRoman(int num) {
315+
static char ans[20];
316+
ans[0] = '\0';
317+
for (int i = 0; i < 13; ++i) {
318+
while (num >= vs[i]) {
319+
num -= vs[i];
320+
strcat(ans, cs[i]);
321+
}
319322
}
320-
}
321-
return res;
323+
return ans;
322324
}
323-
324325
```
325326

326327
<!-- tabs:end -->

0 commit comments

Comments
 (0)