当前位置:网站首页>【leetcode】150 逆波兰表达式求值
【leetcode】150 逆波兰表达式求值
2022-07-20 05:53:00 【慕雪华年】
题目来源
思路
逆波兰表达式又称为后缀表达式
- 中缀
1 + 2 * 3
- 后缀
1 2 3 + *
我们需要将逆波兰表达式化为正常可计算的中缀表达式,再进行计算。题目所给的参数是后缀表达式,其操作的思路如下:
- 遇到操作数,入栈
- 遇到运算符,取栈顶两个连续数据进行计算,再将计算结果入栈
看起来不难,是因为这道题已经是简化后的版本,其所给后缀表达式中没有出现()
这种特殊优先级的操作。下面说一下把中缀转后缀的思路(本题没有涉及)
如何将中缀表达式转为后缀?
以这个中缀表达式为例
1 + 2 * 3 / 2 -5
我们都知道,运算顺序应该是先计算2*3
然后在计算6/2
,最后计算1+3-5
得出结果-1
因为* /
操作符的优先级高于加减,这里就需要注意这种情况。我们需要用一个栈来存放操作符
- 遇到操作数的时候 先输出
- 遇到操作符,和栈顶进行比较
- 如果栈为空/操作符优先级高于栈顶,入栈
- 操作符优先级低于栈顶或和栈顶相同,出栈顶操作符
- 最后将栈中的操作符全部出栈,就可以获得后缀表达式
用上面这个思路走一遍,即为下面的情况(不知道这样写的大家能不能看明白)
最终得到的结果如下
1 2 3 * 2 / + 5 -
即需要的后缀表达式
我们可以用本题代码测试一下这个用例,得出的结果也是-1
,正确!
完整代码
//https://leetcode.cn/problems/evaluate-reverse-polish-notation/submissions/
//150逆波兰表达式
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> s;
for(auto& ch : tokens)
{
if(ch=="+"||ch=="-"||ch=="*"||ch=="/")
{
int right=s.top();
s.pop();
int left=s.top();
s.pop();
switch(ch[0])
{
case '+':
s.push(left+right);
break;
case '-':
s.push(left-right);
break;
case '*':
s.push(left*right);
break;
case '/':
s.push(left/right);
break;
default:
break;
}
}
else{
s.push(stoi(ch));
}
}
return s.top();
}
};
边栏推荐
- Reflect的十三个语法学习
- The difference between TCP and UDP
- DNA 6. 基因组变异之绘制精美瀑布图(ComplexHeatmap)
- TCP中的三次握手和四次断开
- JS笔试题--Promise,setTimeout,任务队列综合题
- DNA 10. 识别癌症驱动基因 (OncodriveCLUST)
- C語言基本概念——每天一遍小知識
- Analysis of advantages and disadvantages of Beijing double line machine room
- TCP和UDP的区别
- Network security architecture: axiom of security architecture
猜你喜欢
JS基础--Data
FigDraw 11. SCI 文章绘图之小提琴图 (ViolinPlot)
与传统IT开发相比,低代码平台有何优势?
Some analysis of Beijing double line machine room
Extract the characters in the list in comma separated form
DNA 6. 基因组变异之绘制精美瀑布图(ComplexHeatmap)
SCS【1】今天开启单细胞之旅,述说单细胞测序的前世今生
Static routing - Comprehensive Experiment
The latest upx3.91 supports win64 / PE plus / minus shell
Development history of Beijing BGP machine room
随机推荐
Concepts de base de la langue C - petites connaissances une fois par jour
JS基础--JSON
Solve tensoflow2 No module named:tensorflow contrib
Super dry goods: design summary, tools and technical points of data visualization are all available
落幕,致我的大学生活
Reptile analysis process from a rookie (code attached at the end of the text)
JS笔试题--正则表达式
Fun guessing game (not binary search! Four digits)
C語言基本概念——每天一遍小知識
The difference between TCP and UDP
Keep updating the article I'm back
JS笔试题--浏览器内核
Introduction to microservices
JS笔试题--对象的深拷贝
DOM操作--获取元素和节点
Excel中实用的3个数据透视表操作技巧,简单高效!
DNA 9. Uncover the correlation between tumor heterogeneity and TMB, MSI
DNA 10. 识别癌症驱动基因 (OncodriveCLUST)
String length of C language programming skills
Access数据库对象包括哪六个?Access与 Excel 最重要的区别是什么?