-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.roman-to-integer.cpp
76 lines (73 loc) · 1.97 KB
/
13.roman-to-integer.cpp
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
/*
* @lc app=leetcode id=13 lang=cpp
*
* [13] Roman to Integer
*/
// @lc code=start
class Solution {
public:
int romanToInt(string s) {
int result = 0;
for (int i = 0; i < s.size(); i++) {
if(s.at(i) == 'I') {
if(i != s.size() - 1) {
if(s.at(i + 1) == 'V') {
result += 4;
i++;
continue;
}
else if(s.at(i + 1) == 'X') {
result += 9;
i++;
continue;
}
}
result += 1;
}
if(s.at(i) == 'V') {
result += 5;
}
if(s.at(i) == 'X') {
if(i != s.size() - 1) {
if(s.at(i + 1) == 'L') {
result += 40;
i++;
continue;
}
else if(s.at(i + 1) == 'C') {
result += 90;
i++;
continue;
}
}
result += 10;
}
if(s.at(i) == 'L') {
result += 50;
}
if(s.at(i) == 'C') {
if(i != s.size() - 1) {
if(s.at(i + 1) == 'D') {
result += 400;
i++;
continue;
}
else if(s.at(i + 1) == 'M') {
result += 900;
i++;
continue;
}
}
result += 100;
}
if(s.at(i) == 'D') {
result += 500;
}
if(s.at(i) == 'M') {
result += 1000;
}
}
return result;
}
};
// @lc code=end