C++ easy to understand solutions.


  • 1
    C
    int romanToInt1(string s) {
        int arabic[] = {1, 5, 10, 50, 100, 500, 1000};
        char roman[] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int idx = find(roman, roman+sizeof(roman)/sizeof(roman[0]), s.back())-roman;
        int res = arabic[idx];
        for (unsigned int i = 1; i < s.size(); i++) {
            int idx1 = find(roman, roman+sizeof(roman)/sizeof(roman[0]), s[i-1])-roman;
            int idx2 = find(roman, roman+sizeof(roman)/sizeof(roman[0]), s[i])-roman;
            if (arabic[idx1] < arabic[idx2])
                res -= arabic[idx1];
            else
                res += arabic[idx1];
        }
        return res;
    }
    
    int romanToInt(string s) {
        unordered_map<char, int> myMap = {{'I',1}, {'V',5}, {'X',10}, {'L',50}, {'C',100}, {'D',500}, {'M',1000}};
        int res = myMap[s.back()];
        for (unsigned int i = 1; i < s.size(); i++) {
            if (myMap[s[i-1]] < myMap[s[i]])
                res -= myMap[s[i-1]];
            else
                res += myMap[s[i-1]];
        }
        return res;
    }

Log in to reply
 

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