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>myMap;
myMap['M'] = 1000;
myMap['D'] = 500;
myMap['C'] = 100;
myMap['L'] = 50;
myMap['X'] = 10;
myMap['V'] = 5;
myMap['I'] = 1;
int result = 0,temp = 0,pre = 0;
for(int i = 0; i < s.length(); i++){
if((i + 1) < s.length() && myMap[s[i+1]] > myMap[s[i]]){
result -= myMap[s[i]];
}
else{
result += myMap[s[i]];
}
}
if(result < 1 || result > 3999){
return NULL;
}
return result;
}
};