Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
有关罗马的常识:
I--1,V--5,X--10,L--50,C-100,D--500,M--1000
前<后,加上前面的数。前>后,减去前面的数。
class Solution {
public:
int romanToInt(string s) {
int data[256]={0};
data['I']=1;
data['V']=5;
data['X']=10;
data['L']=50;
data['C']=100;
data['D']=500;
data['M']=1000;
int nLen=s.length()-1;
int nResult=data[s[nLen]];
for(int i=nLen;i>=1;--i)
{
if(data[s[i]] > data[s[i-1]])
{
nResult-=data[s[i-1]];
}
else
{
nResult+=data[s[i-1]];
}
}
return nResult;
}
};