c++ map


  • 0

    Using map

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

Log in to reply
 

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