广州贝勤网络科技有限公司,女装标题优化关键词,设计师做画册必备网站,学会wordpress建站题目链接
力扣#xff08;LeetCode#xff09;官网 - 全球极客挚爱的技术成长平台 题目解析 首先我们需要知道什么是逆波兰表达式#xff0c;像我们平常遇到的都是中缀表达式#xff0c;然而逆波兰确实后缀表达式#xff0c;因此这个题目隐含的意思就是将一个后缀表达式转…题目链接
力扣LeetCode官网 - 全球极客挚爱的技术成长平台 题目解析 首先我们需要知道什么是逆波兰表达式像我们平常遇到的都是中缀表达式然而逆波兰确实后缀表达式因此这个题目隐含的意思就是将一个后缀表达式转换为中缀表达式并计算它的值。 本题使用栈来进行存储遍历到的数据当我们遍历到数字的时候将该数字入栈如果遍历到运算符就出栈两个数进行运算符的操作然后将结果入栈。最终剩下的栈顶元素就是我们需要的答案。 代码
class Solution
{
public:int evalRPN(vectorstring tokens) {// stoi// - * /stackint st;for(auto str:tokens){if(str||str-||str*||str/){int rightst.top();st.pop();int leftst.top();st.pop();switch(str[0]){case :{int retleftright;st.push(ret);break;}case -:{int retleft-right;st.push(ret);break;}case *:{int retleft*right;st.push(ret);break;}case /:{int retleft/right;st.push(ret);}default:{}}}else{int targetstoi(str);st.push(target);}}return st.top();}
};