We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7507f51 commit 0acb43cCopy full SHA for 0acb43c
roman_to_integer.cpp
@@ -0,0 +1,29 @@
1
+class Solution {
2
+public:
3
+ int romanToInt(string s) {
4
+ map<char, int> pmap;
5
+ pmap['M'] = 1000;
6
+ pmap['D'] = 500;
7
+ pmap['C'] = 100;
8
+ pmap['L'] = 50;
9
+ pmap['X'] = 10;
10
+ pmap['V'] = 5;
11
+ pmap['I'] = 1;
12
+
13
+ int sum = 0;
14
+ for (auto it = s.begin(); it != s.end(); ++it)
15
+ {
16
+ auto next = it + 1;
17
+ if (pmap[*it] < pmap[*next])
18
19
+ sum += pmap[*next] - pmap[*it];
20
+ ++it;
21
+ }
22
+ else
23
24
+ sum += pmap[*it];
25
26
27
+ return sum;
28
29
+};
0 commit comments