Simple 56ms C++ solution


  • 12
    D

    Processing the roman number from right to left turns out to be a bit easier since we can easily tell when to add or subtract:

    class Solution {
    public:
        int romanToInt(string s) {
            if (s.empty()) { return 0; }
            unordered_map<char, int> mp { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} };
            int sum = mp[s.back()];
            for (int i = s.size() - 2; i >= 0; --i) {
                sum += mp[s[i]] >= mp[s[i + 1]] ? mp[s[i]] : -mp[s[i]];
            }
            return sum;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.