#include <cctype>
#include <sstream>
#include <stack>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param tokens string字符串vector
     * @return int整型
     */
    stack<int>ss;
    int sum = 0, cnt = 0;
    int calculatePostfix(vector<string>& tokens) {
        // write code here
        //cout<<tokens[cnt]<<endl;
        int ls = tokens.size();
        while (cnt < ls) {
            string a = tokens[cnt];
            if (a == "-" || a == "+" || a == "*" || a == "/") {
                int c = ss.top();
                ss.pop();
                int b = ss.top();
                ss.pop();
                if (a == "-")
                    ss.push(b - c);
                if (a == "+")
                    ss.push(b + c);
                if (a == "*")
                    ss.push(b * c);
                if (a == "/")
                    ss.push(b / c);
                // cout<<b<<' '<< a<<' '<< c<<endl;
            } else ss.push(stoi(a));
            ++cnt;
        }
        return ss.top();
    }
};

一、题目考察的知识点

栈+模拟

二、题目解答方法的文字分析

遇到数字就存入栈中,遇到符号就取出最上面两个数字,用后面的数字去计算前面的数字,然后把计算的结果存入栈中。循环结束之后栈顶的元素就是计算答案。

三、本题解析所用的编程语言

c++