Roman to Integer C++ solution


  • 0
    Y

    class Solution {
    public:
    int romanToInt(string s) {
    unordered_map<char,int> ma{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};
    int ret=0;
    int temp;
    for(string::iterator it=s.begin();it<s.end();it++){
    temp=ma[it];
    if(it==s.end()-1 || ma[
    (it+1)]<=ma[*it]){
    ret=ret+temp;
    }
    else{
    ret=ret-temp;
    }
    }

        return ret;
        
        
        
    }
    

    };


Log in to reply
 

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