当前位置:网站首页>LeetCode_栈_中等_150. 逆波兰表达式求值
LeetCode_栈_中等_150. 逆波兰表达式求值
2022-06-26 12:37:00 【一瓢江湖我沉浮】
1.题目
根据逆波兰表示法,求表达式的值。
有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
注意 两个整数之间的除法只保留整数部分。
可以保证给定的逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 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 + * 也可以依据次序计算出正确结果。
② 适合用栈操作运算:遇到数字则入栈;遇到算符则取出栈顶两个数字进行计算,并将结果压入栈中
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/evaluate-reverse-polish-notation
2.思路
(1)栈
本题直接将逆波兰表达式给出来,只需按照计算逆波兰表达式的步骤写出代码即可,在该过程中需要用到栈这一数据结构。
① 定义一个栈 stack ,用于存放中间的计算结果,当栈中只剩一个元素时,那么该值就是最终的答案;
② 开始遍历 tokens,对于每个 token,进行以下判断:
1)如果当前 token 是运算符,即 +、-、*、/中的一种(设为 op),那么取出最接近栈顶的两个元素(分别设为 num1 和 num2), 并将 num2 op num1 的结果入栈;
2)如果当前标记为整数,则将其入栈;
③ tokens 遍历结束后,返回栈顶元素即可。
3.代码实现(Java)
//思路1————栈
class Solution {
public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String str : tokens) {
if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
/* 当前标记为运算符,设最接近栈顶的两个元素分别为 num1 和 num2 将 num2 op num1 的结果入栈 */
int num1 = stack.pop();
int num2 = stack.pop();
int tmpRes;
if (str.equals("+")) {
tmpRes = num2 + num1;
} else if (str.equals("-")) {
tmpRes = num2 - num1;
} else if (str.equals("*")) {
tmpRes = num2 * num1;
} else {
tmpRes = num2 / num1;
}
stack.push(tmpRes);
} else {
//当前标记为整数,将其入栈
stack.push(Integer.parseInt(str));
}
}
return stack.pop();
}
}
边栏推荐
- 710. random numbers in the blacklist
- solo 博客系统的 rss 渲染失败
- 无人机遥感在森林监测的部分应用研究案例总结
- 大智慧哪个开户更安全,更好点
- Mysql8 master-slave replication
- 国标GB28181协议EasyGBS级联宇视平台,保活消息出现403该如何处理?
- JS get the current screen height method and listen for DOM elements to enter the viewport
- dried food! Yiwen will show you SD card, TF card and SIM card!
- MS17_ 010 utilization summary
- Redis learning - 05 node JS client operation redis and pipeline pipeline
猜你喜欢

NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools

Less than 40 lines of code to create a blocprovider

Spark-day01- get started quickly

Implementing mixins scheme in applet

JS get the current screen height method and listen for DOM elements to enter the viewport

深入解析 MySQL binlog

Scala problem solving the problem of slow SBT Download

Tiger DAO VC产品正式上线,Seektiger生态的有力补充

【Spark】.scala文件在IDEA中几种图标的解释

Spark-day02-core programming-rdd
随机推荐
el-form-item 包含两个input, 校验这两个input
guacamole安装
NoSQL mongodb - 02 mongodb server installation, mongodb shell, basic concepts and visualization tools
NFS shared storage service installation
[solved] laravel completes the scheduled job task (delayed distribution task) [execute a user-defined task at a specified time]
简易数字电路交通灯设计
7-2 大盗阿福
Tiger DAO VC产品正式上线,Seektiger生态的有力补充
Mysql8 master-slave replication
KITTI Tracking dataset whose format is letf_top_right_bottom to JDE normalied xc_yc_w_h
Less than 40 lines of code to create a blocprovider
Investment planning and forecast report on the future direction of China's smart agriculture during the 14th five year plan (2022)
NLP-D60-nlp比赛D29
Several methods added to the ES6 array (foreach, filter, some, every. Includes, reduce)
Research on the current situation of China's modified engineering plastics market and demand forecast analysis report 2022-2028
The laravel dingo API returns a custom error message
不到40行代码手撸一个BlocProvider
关于NaN的一些总结
做自媒体视频的各种常用工具合集奉上
7-1 n皇后问题