-
Notifications
You must be signed in to change notification settings - Fork 0
/
leet_0013_roman_to_int___voldemort_group.c
107 lines (88 loc) · 2.08 KB
/
leet_0013_roman_to_int___voldemort_group.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Symbol Value
I 1 . . case V . case X
V 5 . priority
X 10 . . case L . case C
L 50 . priority
C 100 . . case D . case M
D 500 . priority
M 1000 . priority
*/
int romanToInt(char * s)
{
int res = 0;
int i = 0;
char c, c2;
while (s[i])
{
c = s[i];
/* priority group : V L D M ( the Voldemort group ) */
if (c == 'M')
res += 1000;
else if (c == 'D')
res += 500;
else if (c == 'L')
res += 50;
else if (c == 'V')
res += 5;
/* four-nine group : either 4 or 9 ( or basecase of 1 ) */
/* CD . CM . just C */
else if (c == 'C')
{
if (s[i + 1] && s[i + 1] == 'D')
{
res += 400;
i += 1;
}
else if (s[i + 1] && s[i + 1] == 'M')
{
res += 900;
i += 1;
}
// basecase just c
else
{
res += 100;
}
}
/* XL . XC . just X */
else if (c == 'X')
{
if (s[i + 1] && s[i + 1] == 'L')
{
res += 40;
i += 1;
}
else if (s[i + 1] && s[i + 1] == 'C')
{
res += 90;
i += 1;
}
// basecase just x
else
{
res += 10;
}
}
/* IV . IX . just I */
else if (c == 'I')
{
if (s[i + 1] && s[i + 1] == 'V')
{
res += 4;
i += 1;
}
else if (s[i + 1] && s[i + 1] == 'X')
{
res += 9;
i += 1;
}
else
{
res += 1;
}
}
i += 1;
}
return res;
}