企业自助建站系统怎么建,安徽网新科技有限公司怎么样,重新下载一个微信,西宁做网站君博认同栈
思路#xff1a; 使用栈存放运算数#xff1b;遍历 tokens#xff0c;当遇到运算符时#xff0c;2 次出栈得到运算数进行相应的运算#xff0c;将运算结果入栈#xff1b;最终的结果在栈顶上#xff1b;
class Solution {
public:int evalRPN(vectorstring…栈
思路 使用栈存放运算数遍历 tokens当遇到运算符时2 次出栈得到运算数进行相应的运算将运算结果入栈最终的结果在栈顶上
class Solution {
public:int evalRPN(vectorstring tokens) {std::stackint stk;int size tokens.size();for (int idx 0; idx size; idx) {std::string token tokens[idx];if (isNumber(token)) {stk.push(atoi(token.c_str()));} else {int num2 stk.top();stk.pop();int num1 stk.top();stk.pop();switch (token[0]) {case :stk.push(num1 num2);break;case -:stk.push(num1 - num2);break;case *:stk.push(num1 * num2);break;case /:stk.push(num1 / num2);break;}}}return stk.top();}private:bool isNumber(const std::string s) {return !((s ) || (s -) || (s *) || (s /));}
};