Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
class Solution {
public:
int romanToInt(string s)
{
int n = s.length();
map<char, int> m;
m['M'] = 1000;
m['D'] = 500;
m['C'] = 100;
m['L'] = 50;
m['X'] = 10;
m['V'] = 5;
m['I'] = 1;
int ans = 0;
int pre = 0;
for(int i=0; i<n; i++)
{
ans += m[s[i]];
if(m[s[i]] > pre)
ans -= pre << 1;
pre = m[s[i]];
}
return ans;
}
};