偷懒,就在12的基础上改了一下
class Solution { public: int romanToInt(string s) { map<int,string> calc = {{1000,"M"},{900,"CM"},{500,"D"},{400,"CD"},{100,"C"}, {90,"XC"},{50,"L"},{40,"XL"},{10,"X"},{9,"IX"},{5,"V"},{4,"IV"},{1,"I"}}; map<int,string>::reverse_iterator iter=calc.rbegin(); int ret=0; while(iter!=calc.rend()) { int len=(iter->second).length(); if(len>s.length()) iter++; else if(s.substr(0,len)==iter->second){ ret+=iter->first; s=s.substr(len); } else iter++; } return ret; } };