13. 罗马数字转整数
程序员文章站
2023-12-27 18:24:15
...
够简洁
class Solution {
public:
int romanToInt(string s) {
vector<int> backet(150);
backet['I'] = 1;
backet['V'] = 5;
backet['X'] = 10;
backet['L'] = 50;
backet['C'] = 100;
backet['D'] = 500;
backet['M'] = 1000;
int ans = 0;
for(int i = 0; i < s.size(); i++) {
if(i != s.size() - 1 && backet[s[i]] < backet[s[i + 1]]){
ans -= backet[s[i]];
}
else ans += backet[s[i]];
}
return ans;
}
};