[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.

Solution:

class Solution {
public:
    int romanToInt(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int length = s.length();
        if(length <1) return 0;
        map<char,int> m;
        m['I'] = 1;
        m['V'] = 5;
        m['X'] = 10;
        m['L'] = 50;
        m['C'] = 100;
        m['D'] = 500;
        m['M'] = 1000;
        int i = length-1;
        int sum = m[s[i--]];
        while(i>=0)
            if(m[s[i+1]] > m[s[i]]) //相邻位的大小关系,决定转换法则
                sum -= m[s[i--]];
            else
                sum += m[s[i--]];
        return sum;
    }
};
原文地址:https://www.cnblogs.com/changchengxiao/p/3687484.html