leetcode150_逆波兰表达式求值_栈

1. 总体思路,定义数据栈存放数据, 只要遇到运算符就出栈两个数作加减乘除处理.spa

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for(int i=0;i<tokens.size();i++) {
            if(tokens[i]!="+" && tokens[i]!="-" && tokens[i]!="*" && tokens[i]!="/") {
                int num = stoi(tokens[i]);
                st.push(num);
            }
            else {
                int second = st.top(); st.pop();
                int first = st.top(); st.pop();
                int all = 0;
                if(tokens[i]=="+") all = first+second;
                if(tokens[i]=="-") all = first-second;
                if(tokens[i]=="*") all = first*second;
                if(tokens[i]=="/") all = first/second;
                st.push(all);
            }
        }
        return st.top();
    }
};