-
Notifications
You must be signed in to change notification settings - Fork 0
/
13. Roman to Integer
89 lines (80 loc) · 2.18 KB
/
13. Roman to Integer
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
Runtime Error
Line 1240: Char 9: runtime error: addition of unsigned offset to 0x7fd042900140 overflowed to 0x7fd04290013f (basic_string.h)
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/bits/basic_string.h:1249:9
Last Executed Input
Open Testcase
s =
"III"
Solution of error : i > 0 &&
class Solution {
public:
int romanToInt(string s) {
int sum = 0;
for (int i = s.length() - 1; i >= 0; i--) {
switch (s[i]) {
case 'I':
if(i> 0 && s[i - 1] == 'V')
{
sum += 6;
i--;
}
else
{
sum += 1;
}
break;
case 'V':
if (i > 0 && s[i - 1] == 'I') {
sum += 4;
i--;
} else {
sum += 5;
}
break;
case 'X':
if (i > 0 && s[i - 1] == 'I') {
sum += 9;
i--;
} else {
sum += 10;
}
break;
case 'L':
if (i > 0 && s[i - 1] == 'X') {
sum += 40;
i--;
} else {
sum += 50;
}
break;
case 'C':
if (i > 0 && s[i - 1] == 'X') {
sum += 90;
i--;
} else {
sum += 100;
}
break;
case 'D':
if (i > 0 && s[i - 1] == 'C') {
sum += 400;
i--;
} else {
sum += 500;
}
break;
case 'M':
if (i > 0 && s[i - 1] == 'C') {
sum += 900;
i--;
} else {
sum += 1000;
}
break;
default:
break;
}
}
return sum;
}
};