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 getVal(char a)
{
switch (a) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return 0;
}
int romanToInt(string s) {
int a=0;
int max = 1;
for (int i = s.length() - 1; i >= 0; i--) {
if (getVal(s[i]) >= max)
{
a += getVal(s[i]);
max = getVal(s[i]);
}
else {
a -= getVal(s[i]);
}
}
return a;
}
};