Solution: Generalized the roman char in the format of number


#1

Solution>>>>>
int Solution::romanToInt(string A) {

    vector<int> toNum(string a);
    vector<int> a;
    a=toNum(A);
    map <int,int> mp;
    mp[1]=1;
    mp[12]=4;
    mp[2]=5;
    mp[13]=9;
    mp[3]=10;
    mp[34]=40;
    mp[4]=50;
    mp[35]=90;
    mp[5]=100;
    mp[56]=400;
    mp[6]=500;
    mp[57]=900;
    mp[7]=1000;
    
    int ans=0;
    for(int i=0;i<a.size();i++){
        int temp=0;
        temp=a[i];
        while(a[i+1]>a[i] && i+1<a.size()){
            temp=temp*10+a[i+1];
            i++;
        }
        ans+=mp[temp];
    }
    return ans;
    
}
vector<int> toNum(string a){
    vector<int> v;
    map <char,int> m;
    m['I']=1;
    m['V']=2;
    m['X']=3;
    m['L']=4;
    m['C']=5;
    m['D']=6;
    m['M']=7;
    for(auto x:a){
        v.push_back(m[x]);
    }
    return v;
}