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) {
unordered_map<char, char> mv = {{'X', 'L'}, {'C', 'D'}, {'I', 'V'}};
unordered_map<char, char> mm = {{'C', 'M'}, {'X', 'C'}, {'I', 'X'}};
unordered_map<char, int> mvalue = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
int size = s.size();
int result = 0;
for (int i = 0; i < size; ++i)
{
int j = i + 1;
if (j < size && mv[s[i]] == s[j])
{
result += mvalue[s[i]] * 4;
i = j;
}
else if (j < size && mm[s[i]] == s[j])
{
result += mvalue[s[i]] * 9;
i = j ;
}
else
result += mvalue[s[i]];
}
return result;
}
};