天天看點

leetcode---roman-to-integer---字元串

Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

class Solution {
public:
    int romanToInt(string s) 
    {
        int n = s.length();

        map<char, int> m;
        m['M'] = ;
        m['D'] = ;
        m['C'] = ;
        m['L'] = ;
        m['X'] = ;
        m['V'] = ;
        m['I'] = ;

        int ans = ;
        int pre = ;
        for(int i=; i<n; i++)
        {
            ans += m[s[i]];
            if(m[s[i]] > pre)
                ans -= pre << ;
            pre = m[s[i]];

        }
        return ans;
    }
};