【c++】leetcode150. 逆波兰表达式求值

class Solution {
public:
    int evalRPN(vector& tokens) {

        stack st;
        for (int i = 0; i < tokens.size(); i++)
        {
            if(tokens[i] == "+" || tokens[i] == "-" 
            || tokens[i] == "*" || tokens[i] == "/")
            {
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                if(tokens[i] == "+")
                {
                    st.push(left + right);
                }
                else if(tokens[i] == "-")
                {
                    st.push(left - right);
                }
                else if(tokens[i] == "*")
                {
                    st.push(left * right);
                }
                else if(tokens[i] == "/")
                {
                    st.push(left / right);
                }
            }
            else
            {
                st.push(stoi(tokens[i]));
            }
        }


        return st.top();

    }
};

你可能感兴趣的:(c++,算法,leetcode)