Leetcode 13. 罗马数字转整数

按照罗马数字规则
对每一位:其右边大,减去这个数;其右边小,加上这个数
最后再加上最右边的数

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

你可能感兴趣的:(Leetcode 13. 罗马数字转整数)