问题描述
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
罗马数字转数字,如VII=7
只要了解规则即可
代码
class Solution {
private:
map<char, int> m;
public:
Solution(){
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
}
int romanToInt(string s) {
int r = m[s[0]];
for(int i=1;i<s.size();i++){
int tmp = m[s[i]];
if(tmp <= m[s[i-1]]){
r += tmp;
} else {
r -= m[s[i-1]];
r += tmp - m[s[i-1]];
}
}
return r;
}
};