方法一:
class Solution {
public:
int romanToInt(string s) {
int len=s.size();
map<string,int> number{
{"I",1},
{"V",5},
{"X",10},
{"L",50},
{"C",100},
{"D",500},
{"M",1000},
{"IV",4},
{"IX",9},
{"XL",40},
{"XC",90},
{"CD",400},
{"CM",900}
};
string s1;
string s2;
string s3;
int sum=0;
int i=0;
for(i;i<=len-2;)
{
s1=s.substr(i,1);
s2=s.substr(i+1,1);
if(number.at(s1)<number.at(s2))
{
s3=s1+s2;
sum=number.at(s3)+sum;
i=i+2;
}
else
{
sum=number.at(s1)+sum;
i++;
}
}
if(i==len-1)
{
sum=number.at(s.substr(len-1,1))+sum;
}
return sum;
}
};