文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
class Solution {
public:
int romanToInt(string s) {
map<char, int> values;
values['I'] = 1;
values['V'] = 5;
values['X'] = 10;
values['L'] = 50;
values['C'] = 100;
values['D'] = 500;
values['M'] = 1000;
int value = 0;
for(int i = 0; i < s.length() - 1; i++) {
if(values[s[i]] < values[s[i + 1]]) {
value -= values[s[i]];
}
else {
value += values[s[i]];
}
}
value += values[s[s.length() - 1]];
return value;
}
};