题目描述
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
代码
class Solution {
public:
int romanToInt(string s) {
map<char,int>dig;
dig['I']=1;
dig['X']=10;
dig['C']=100;
dig['M']=1000;
dig['V']=5;
dig['L']=50;
dig['D']=500;
int ans = 0,b=0;
for(int i=s.size()-1;i>=0;--i)
{
if(dig[s[i]]>=b)
{
ans+=dig[s[i]];
}
else
{
ans-=dig[s[i]];
}
b=dig[s[i]];
}
return ans;
}
};