C++ Solution 20ms


  • 0
    N

    Nothing special except I used c library function.

    Is there any other function like "isdigit()" in C++?

    class Solution {
        bool numcheck(const string &str)
        {
            for(int i=0; i<str.length(); i++)
                if(isdigit(str[i]) != 0) return true;
            return false;
        }
    public:
        int evalRPN(vector<string>& tokens) {
            stack<int> st;
            for(int i=0; i<tokens.size(); i++)
            {
                if(numcheck(tokens[i])) st.push(stoi(tokens[i]));
                else
                {
                    int a, b;
                    char ch = tokens[i][0];
                    b = st.top();
                    st.pop();
                    a = st.top();
                    st.pop();
                    switch(ch)
                    {
                        case '+': a += b; break;
                        case '-': a -= b; break;
                        case '*': a *= b; break;
                        case '/': a /= b; break;
                    }
                    st.push(a);
                }
            }
            return st.top();
        }
    };

  • 0
    Q
    This post is deleted!

Log in to reply
 

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