My quick straight forward c++ solution, not need to reverse string


  • 0

    '''
    class Solution {
    public:
    int foo(long sum, int a){
    return sum-(sum%a)*2+a ;
    }

    int romanToInt(string s) {
        int sum=0;
        for(int i=0;i<s.length();i++){
            switch(s[i]){
                case 'I': sum += 1;break;
                case 'V': sum = foo(sum,5);break;
                case 'X': sum = foo(sum,10);break;
                case 'L': sum = foo(sum,50);break;
                case 'C': sum = foo(sum,100);break;
                case 'D': sum = foo(sum,500);break;
                case 'M': sum = foo(sum,1000);break;
            };
        }
        return sum;
    }
    

    };
    '''


  • 0

    No need to reverse string, thus, in this way, it can be used in real time computing


Log in to reply
 

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