-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_Roman_to_Integer.cpp
More file actions
44 lines (44 loc) · 1.16 KB
/
13_Roman_to_Integer.cpp
File metadata and controls
44 lines (44 loc) · 1.16 KB
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
class Solution {
public:
int romanToInt(string s) {
std::deque<int> sto;
for (const auto& c : s) {
switch (c) {
case 'I':
sto.push_back(1);
break;
case 'V':
sto.push_back(5);
break;
case 'X':
sto.push_back(10);
break;
case 'L':
sto.push_back(50);
break;
case 'C':
sto.push_back(100);
break;
case 'D':
sto.push_back(500);
break;
case 'M':
sto.push_back(1000);
break;
}
}
int result = sto.back();
int buffer = sto.back();
sto.pop_back();
while (!sto.empty()) {
if (sto.back() < buffer) {
result -= sto.back();
} else {
result += sto.back();
}
buffer = sto.back();
sto.pop_back();
}
return result;
}
};