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