Roman to Integer

class Solution {
public:
    int romanToInt(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        map<char,int> m;
        m['I'] = 1;
        m['V'] = 5;
        m['X'] = 10;
        m['L'] = 50;
        m['D'] = 500;
        m['C'] = 100;
        m['M'] = 1000;
        int pre = 2000;
        int result = 0;
        for(int i = 0;i < s.length();i++)
        {
            if(pre < m[s[i]])
            {
                result = result + m[s[i]] - pre*2;
            }
            else
            {
                result = result + m[s[i]];
            }
            pre = m[s[i]];
        }
        return result;
    }
};

原文地址:https://www.cnblogs.com/727713-chuan/p/3306110.html