当前位置:网站首页>LeetCode 0150. 逆波兰表达式求值
LeetCode 0150. 逆波兰表达式求值
2022-08-01 05:23:00 【Tisfy】
【LetMeFly】150.逆波兰表达式求值
力扣题目链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation/
根据 逆波兰表示法,求表达式的值。
有效的算符包括 +
、-
、*
、/
。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
注意 两个整数之间的除法只保留整数部分。
可以保证给定的逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
示例 1:
输入:tokens = ["2","1","+","3","*"] 输出:9 解释:该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
示例 2:
输入:tokens = ["4","13","5","/","+"] 输出:6 解释:该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6
示例 3:
输入:tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] 输出:22 解释:该算式转化为常见的中缀算术表达式为: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22
提示:
1 <= tokens.length <= 104
tokens[i]
是一个算符("+"
、"-"
、"*"
或"/"
),或是在范围[-200, 200]
内的一个整数
逆波兰表达式:
逆波兰表达式是一种后缀表达式,所谓后缀就是指算符写在后面。
- 平常使用的算式则是一种中缀表达式,如
( 1 + 2 ) * ( 3 + 4 )
。 - 该算式的逆波兰表达式写法为
( ( 1 2 + ) ( 3 4 + ) * )
。
逆波兰表达式主要有以下两个优点:
- 去掉括号后表达式无歧义,上式即便写成
1 2 + 3 4 + *
也可以依据次序计算出正确结果。 - 适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中
方法一:栈模拟
如果懂了什么是逆波兰表达式,那么这道题将会非常简单。
逆波兰表达式的计算要比求表达式的逆波兰容易得多。
使用一个栈,
遍历逆波兰表达式,如果遇到运算符,就从栈中取出对应个数的元素,并进行运算,再把结果入栈。
例如,如果遇到了
+
,就从栈中取出两个元素(因为加号是双目运算符),求和并将结果入栈。
注意,栈中的顺序与原顺序是反着的,先出栈的是位置较后的元素。
如果遇到数字,就之间入栈。
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是逆波兰表达式中的元素/运算符个数
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for (string& s : tokens) {
if (s == "+" || s == "-" || s == "*" || s == "/") {
int second = st.top();
st.pop();
int first = st.top();
st.pop();
if (s == "+")
st.push(first + second);
else if (s == "-")
st.push(first - second);
else if (s == "*")
st.push(first * second);
else if (s == "/")
st.push(first / second);
}
else {
st.push(atoi(s.c_str()));
}
}
return st.top();
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126084278
边栏推荐
- Selenium:上传、下载文件
- MySQL-DML language-database operation language-insert-update-delete-truncate
- y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
- Qt Widget 项目对qml的加载实例
- pytorch、tensorflow对比学习—功能组件(优化器、评估指标、Module管理)
- 挑战52天背完小猪佩奇(第01天)
- MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
- 图片更新之后Glide加载依旧是原来的图片问题
- 说说js中使用for in遍历数组存在的bug
- state compressed dp
猜你喜欢
Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums
SL-12/2过流继电器
I met a shell script
leetcode43 string multiplication
万字逐行解析与实现Transformer,并进行德译英实战(一)
USB3.0:VL817Q7-C0的LAYOUT指南(二)
About making a progress bar for software initialization for Qt
Robot growth in China
(2022牛客多校四)H-Wall Builder II(思维)
pytorch、tensorflow对比学习—计算图和微分机制
随机推荐
WPF项目-初步了解数据绑定 binding
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (2)
MySQL-Data Operation-Group Query-Join Query-Subquery-Pagination Query-Joint Query
字符中的第一个唯一字符
Robot_Framework:关键字
备战金九银十,如何顺利通过互联网大厂Android的笔面试?
用控件当画笔获得bitmap代码记录
Flip letters using string container
LeetCode 9. 回文数
ORACLE 实现另外一个用户修改包(package)
NUMPY
Robot_Framework: keyword
pytroch、tensorflow对比学习—搭建模型范式(低阶、中阶、高阶API示例)
Selenium: JS operation
Malicious attacks on mobile applications surge by 500%
对话MySQL之父:一个优秀程序员可抵5个普通程序员
(2022 Niu Ke Duo School IV) N-Particle Arts (Thinking)
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Jupyter shortcuts
matplotlib pyplot