题目:
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) {
map<char, int> romap;
romap['I'] = 1, romap['V'] = 5, romap['X'] = 10;
romap['L'] = 50, romap['C'] = 100, romap['D'] = 500, romap['M'] = 1000;
int n = s.size();
if(n == 0)
return -1;
int result = romap[s[n-1]];
for(int i = n-2; i >= 0; i--) {
if(romap[s[i]] < romap[s[i+1]])
result -= romap[s[i]];
else
result += romap[s[i]];
}
return result;
}
};