more concise C++ solution


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

Log in to reply
 

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