LCR036.逆波兰表达式求值
链接:LCR036.逆波兰表达式求值
难度:Medium
标签:栈、数组、数学
简介:根据 逆波兰表示法,求该后缀表达式的计算结果。
题解 1 - cpp
- 编辑时间:2021-12-23
- 执行用时:12ms
- 内存消耗:11.6MB
- 编程语言:cpp
- 解法介绍:栈存储。
class Solution {
   public:
    int s2i(string str) {
        int ans = 0, f = 1;
        for (int i = 0; i < str.size(); i++) {
            if (i == 0 && str[i] == '-') {
                f = -1;
                continue;
            }
            ans = ans * 10 + str[i] - '0';
        }
        return ans * f;
    }
    int evalRPN(vector<string>& tokens) {
        stack<int> s;
        for (auto& str : tokens) {
            if (str == "+" || str == "-" || str == "*" || str == "/") {
                int num1 = s.top();
                s.pop();
                int num2 = s.top();
                s.pop();
                int ans;
                if (str == "+")
                    ans = num2 + num1;
                else if (str == "-")
                    ans = num2 - num1;
                else if (str == "*")
                    ans = num2 * num1;
                else
                    ans = num2 / num1;
                s.push(ans);
            } else {
                s.push(s2i(str));
            }
        }
        return s.top();
    }
};