10-line without stack c++ solution

  • 1
            int evalRPN(vector<string>& tokens) {
                return calc(tokens, tokens.size()-1, *(new int));
            int calc(vector<string>& tokens, int start, int& end) 
                string s=tokens[start];
                if (s=="+" || s=="-" || s=="*" || s=="/") {
                    int A=calc(tokens, start-1, end), B=calc(tokens, end-1, end);
                    if (s=="+") return A+B;
                    if (s=="-") return B-A;
                    if (s=="*") return A*B;
                    if (s=="/") return B/A;
                return stoi(s);

  • 0

    but recursion is equivalent to stack

Log in to reply

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