-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
29 lines (27 loc) · 660 Bytes
/
13.cpp
File metadata and controls
29 lines (27 loc) · 660 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
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
Solution() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}
int romanToInt(string s) {
int sum = 0;
map<char, int> m;
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
for (int index = 0; index < s.size(); index++) {
if (index + 1 < s.size() && m[s[index]] < m[s[index + 1]]) {
sum -= m[s[index]];
} else {
sum += m[s[index]];
}
}
return sum;
}
};