C++ Accepted .. Self explanatory


  • 0
    P

    bool isOperand(string token)
    {
    return ((token == "+") || (token == "-") || (token == "") || (token == "/")) ? true : false;
    }
    int evalRPN(vector<string>& tokens) {
    stack<int> myStack;
    for(auto token: tokens)
    {
    if (isOperand(token))
    {
    int b = myStack.top();
    myStack.pop();
    int a = myStack.top();
    myStack.pop();
    if (token == "+")
    myStack.push(a + b);
    else if (token == "-")
    myStack.push(a - b);
    else if (token == "
    ")
    myStack.push(a * b);
    else if (token == "/")
    myStack.push(a / b);
    }
    else
    myStack.push(stoi(token));
    }
    return myStack.top();
    }


Log in to reply
 

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