代码随想录:逆波兰表达式求值

代码随想录:逆波兰表达式求值

switch纯废物,以后别想着用这个

另外c++的强制转换

stoll //string to long long
stoi //string to int
class Solution {
public:
int evalRPN(vector<string>& tokens) {
        stack<long long> s;

        for (string target : tokens) {
            if (target == "+" || target == "-" || target == "*" || target == "/") {
                long long a = s.top(); s.pop();
                long long b = s.top(); s.pop();
                if (target == "+") {
                    s.push(b + a);
                } else if (target == "-") {
                    s.push(b - a);
                } else if (target == "*") {
                    s.push(b * a);
                } else if (target == "/") {
                    s.push(b / a);
                }
            } else {
                s.push(stoll(target));
            }
        }
        return s.top();
    }
};
posted @ 2024-12-11 23:14  huigugu  阅读(3)  评论(0编辑  收藏  举报