leetcode13 罗马数字转整数

萝らか妹 提交于 2019-12-02 16:40:33

偷懒,就在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;
    }
};

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!