19ms C++ solution which beats 100.00%


  • 0
    B
        int romanToInt(string s) {
            int tmp=0, right=0, left=0;
            for(auto c=s.rbegin();c!=s.rend();c++){
                switch(*c){
                    case 'I': left=1;break;
                    case 'V': left=5;break;
                    case 'X': left=10;break;
                    case 'L': left=50;break;
                    case 'C': left=100;break;
                    case 'D': left=500;break;
                    case 'M': left=1000;
                    default:break;
                }
                left>=tmp ? right+=left : right-=left;
                tmp=left;
            }
            return right;
        }

Log in to reply
 

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