/* * @lc app=leetcode.cn id=150 lang=cpp * * [150] 逆波兰表达式求值 */ // @lc code=start class Solution { public: int calc(int left, int right, char sign) { switch (sign) { case '+': return left + right; case '-': return left - right; case '*': return left * right; case '/': return left / right; } throw ""; } int evalRPN(vector<string> &tokens) { stack<int> intStack; for (string &str : tokens) { if (str.size() == 1 && (str[0] == '+' || str[0] == '-' || str[0] == '*' || str[0] == '/')) { // 遇到运算符,开始运算 int right = intStack.top(); intStack.pop(); int left = intStack.top(); intStack.pop(); intStack.push(calc(left, right, str[0])); } else { // 遇到数字,直接入数字栈 intStack.push(stoi(str)); } } return intStack.top(); } }; // @lc code=end
标签:intStack,right,return,int,波兰,str,求值,表达式,left From: https://www.cnblogs.com/angdh/p/17977097