013 Roman to Integer 罗马数字转整数

给定一个罗马数字,将其转换成整数。

返回的结果要求在 1 到 3999 的范围内。

详见:https://leetcode.com/problems/roman-to-integer/description/

class Solution {
public:
    int romanToInt(string s) {
        int res=0;
        map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
        for(int i=0;i<s.size();++i)
        {
            if(i==s.size()-1||m[s[i+1]]<=m[s[i]])
                res+=m[s[i]];
            else
                res-=m[s[i]];
        }
        return res;
    }
};

 参考:http://www.cnblogs.com/grandyang/p/4120857.html

posted on 2018-03-31 21:34  lina2014  阅读(140)  评论(0编辑  收藏  举报

导航