当前位置:网站首页>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 <= 104tokens[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
边栏推荐
- AspNet.WebApi.Owin 自定义Token请求参数
- Robot_Framework: keyword
- Qt Widget project loading example of qml
- (2022 Nioke Duo School IV) H-Wall Builder II (Thinking)
- Selenium:浏览器操作
- ORACLE 实现另外一个用户修改包(package)
- Jupyter shortcuts
- II. Binary tree to Offer 68 - recent common ancestor
- LeetCode 27. 移除元素
- 2022年超全的Android面经(附含面试题|进阶资料)
猜你喜欢

(more than 2022 cattle school four) A - Task Computing + dynamic programming (sort)

Qt Widget 项目对qml的加载实例

牛客多校2022第四场A,H,K,N

Robot_Framework: commonly used built-in keywords

Code Interview Guide for Programmers CD15 Generating an Array of Windowed Maximums

字符中的第一个唯一字符

(2022 Nioke Duo School IV) H-Wall Builder II (Thinking)

数据湖:数据同步工具NiFi

JWL-11/2-99.9A电流继电器

The sword refers to Offer 68 - I. Nearest Common Ancestor of Binary Search Trees
随机推荐
【音视频】srs直播平台搭建
可视化全链路日志追踪
Selenium:简介
Dialogue with the father of MySQL: One excellent programmer is worth 5 ordinary programmers
可持久化线段树
After the image is updated, Glide loading is still the original image problem
Jupyter shortcuts
Selenium: element judgment
微信小程序获取手机号phonenumber.getPhoneNumber接口开发
WebSocket implements chat function
y83. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (14)
Swastika line-by-line parsing and realization of the Transformer, and German translation practice (a)
I met a shell script
说说js中使用for in遍历数组存在的bug
PaddleX部署推理模型和GUI界面测试结果不一致的解决方法
LeetCode 27. 移除元素
Selenium: mouse, keyboard events
[target detection] YOLOv7 theoretical introduction + practical test
华为Android开发面试后得出的面试秘诀
AspNet.WebApi.Owin 自定义Token请求参数