解法:
class Solution {
public:
map<char, int> roman = {
{'I', 1},
{'V', 5},
{'X', 10},
{'L', 50},
{'C', 100},
{'D', 500},
{'M', 1000}
};
int romanToInt(string s) {
int n = s.length();
int result = 0;
for(int i=0; i<n; i++){
int value = roman[s[i]];
if(i<n-1 && value<roman[s[i+1]]){
result -= value;
}else{
result +=value;
}
}
return result;
}
};