My accepted C++ code


  • 0
    W
    class Solution {
    public:
        int evalRPN(vector<string> &tokens) {
            stack<int> st;
            for(int i=0;i<tokens.size();++i){
                char ch=0;
                if(tokens[i].length()==1)
                    ch=str[0];
                if(ch=='+'||ch=='-'||ch=='*'||ch=='/'){
                    int v1=st.top();
                    st.pop();
                    int v2=st.top();
                    st.pop();
                    switch(ch){
                        case '+':
                        st.push(v2+v1);
                        break;
                        case '-':
                        st.push(v2-v1);
                        break;
                        case '*':
                        st.push(v2*v1);
                        break;
                        case '/':
                        st.push(v2/v1);
                        break;
                    }
                }else{
                    int v=atoi(tokens[i].c_str());
                    st.push(v);
                }
            }
            return st.top();
            
        }
    };

Log in to reply
 

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