1 class Solution {
2 public:
3 int romanToInt(string s) {
4 map<char, int>num;
5 num['I'] = 1;
6 num['V'] = 5;
7 num['X'] = 10;
8 num['L'] = 50;
9 num['C'] = 100;
10 num['D'] = 500;
11 num['M'] = 1000;
12 int sum = num[s[0]];
13 for (int i = 1; i < s.length(); i++)
14 {
15 if (num[s[i - 1]] >= num[s[i]])
16 sum += num[s[i]];
17 else
18 sum = sum + num[s[i]] -2* num[s[i - 1]];
19 }
20 return sum;
21 }
22 };
23