beat 100%,c++ solution


  • 0
        int romanToInt(string s) {
            int res=0;
            if(s.empty())
                return res;
            vector<int> dict(256,0);
            dict['I']=1;
            dict['V']=5;
            dict['X']=10;
            dict['L']=50;
            dict['C']=100;
            dict['D']=500;
            dict['M']=1000;
            int prev=INT_MAX;
            for(int i=0;i<s.size();i++){
                if(dict[s[i]]>prev){
                    res=res-2*prev+dict[s[i]];
                }
                else
                    res=res+dict[s[i]];
                prev=dict[s[i]];
            }
            return res;
        }
    

Log in to reply
 

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