class Solution {
public:
int romanToInt(string s) {
int res=0;
map<char,int>mp={
{'I',1},
{'V',5},
{'X',10},
{'L',50},
{'C',100},
{'D', 500},
{'M', 1000}
};
for(int i=0;i<s.length();i++){
if(mp[s[i]]>=mp[s[i+1]]){
res=res+mp[s[i]];
}
else{
res=res-mp[s[i]];
}
}
return res;
}
};
char字符用‘’,string是“”
参考链接:
https://leetcode-cn.com/problems/roman-to-integer/solution/qing-xi-tu-jie-python3-by-ml-zimingmeng/