-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0013.cpp
More file actions
31 lines (29 loc) · 690 Bytes
/
Copy path0013.cpp
File metadata and controls
31 lines (29 loc) · 690 Bytes
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
class Solution {
public:
int value(char c) {
switch (c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return -10000;
}
}
int romanToInt(string s) {
int size = s.size();
int res = 0;
for (int i = 0; i < size - 1; i++) {
int a = value(s[i]);
int b = value(s[i + 1]);
if (a >= b)
res += a;
else
res -= a;
}
res += value(s[size - 1]);
return res;
}
};