Skip to content

Commit 0acb43c

Browse files
committed
roman_to_integer.cpp
1 parent 7507f51 commit 0acb43c

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

roman_to_integer.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)