Recursive solution


  • 0
    K
    class Solution {
    public:
    
        int romanToInt(string s) {
            printf("%s\n", s.c_str());
        
        #define PROCESS(A,N) \
             { size_t m = s.find_first_of(A);\
            if (m != string::npos){\
                size_t me = s.find_first_not_of(A, m);\
                int n = N * (me == string::npos ? s.length() - m : me - m);\
                printf("%s: A:%c N:%d, m:%d, me:%d, n:%d\n", s.c_str(), A, N, m, me, n);\
                return n - (m ? romanToInt(s.substr(0, m)) : 0) + (me != string::npos ? romanToInt(s.substr(me)) : 0);\
            } }
        
            PROCESS('M', 1000);
            PROCESS('D', 500);
            PROCESS('C', 100);
            PROCESS('L', 50);
            PROCESS('X', 10);
            PROCESS('V', 5);
            PROCESS('I', 1);
            return 0;
        }
    };

Log in to reply
 

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