当前位置:网站首页>leetcode刷题:栈与队列05(逆波兰表达式求值)
leetcode刷题:栈与队列05(逆波兰表达式求值)
2022-07-01 19:16:00 【涛涛英语学不进去】
150. 逆波兰表达式求值
根据 逆波兰表示法,求表达式的值。
有效的运算符包括 + , - , * , / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
说明:
整数除法只保留整数部分。
给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。
示例 1:
- 输入: [“2”, “1”, “+”, “3”, " * "]
- 输出: 9
- 解释: 该算式转化为常见的中缀算术表达式为:((2 + 1) * 3) = 9
示例 2:
- 输入: [“4”, “13”, “5”, “/”, “+”]
- 输出: 6
- 解释: 该算式转化为常见的中缀算术表达式为:(4 + (13 / 5)) = 6
示例 3:
输入: [“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
栈操作:遇到数字就入栈,先出栈的是加数、减数、除数、乘数;后出栈的是被加数、被减数、被除数、被乘数。
遇到数字则入栈、遇到运算符则取出栈顶两个数字进行计算,再将结果压入栈中。
package com.programmercarl.stacks_queues;
import java.util.Stack;
/** * @ClassName EvalRPN * @Descriotion TODO * @Author nitaotao * @Date 2022/6/30 13:26 * @Version 1.0 **/
public class EvalRPN {
public int evalRPN(String[] tokens) {
Stack stack = new Stack();
for (int i = 0; i < tokens.length; i++) {
switch (tokens[i]) {
case "+":
//加数
Integer num1 = Integer.valueOf((String) stack.pop());
//被加数
Integer num2 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num2 + num1));
break;
case "-":
//减数
Integer num3 = Integer.valueOf((String) stack.pop());
//被减数
Integer num4 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num4 - num3));
break;
case "*":
//乘数
Integer num5 = Integer.valueOf((String) stack.pop());
//被乘数
Integer num6 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num6 * num5));
break;
case "/":
//除数
Integer num7 = Integer.valueOf((String) stack.pop());
//被除数
Integer num8 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num8 / num7));
break;
default:
stack.push(tokens[i]);
}
}
return Integer.valueOf((String) stack.pop());
}
}

一遍AC,舒服。!
边栏推荐
- Arduino Stepper库驱动28BYJ-48步进电机测试程序
- Problems encountered in installing MySQL in docker Ubuntu container
- fastDFS入门
- What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
- [multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
- Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
- Use of common built-in classes of JS
- tensorflow 张量做卷积,输入量与卷积核维度的理解
- What else do you not know about new set()
- 【let var const】
猜你喜欢

Common components of flask

目标检测——Yolo系列

数据分析师听起来很高大上?了解这几点你再决定是否转型

朋友圈社区程序源码分享

优质笔记软件综合评测和详细盘点(一) Notion、Obsidian、RemNote、FlowUs

uniapp使用腾讯地图选点 没有window监听回传用户的位置信息,怎么处理

Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it

【Leetcode】最大连续1的个数

Practical project notes (I) -- creation of virtual machine

Richview trvdocparameters page parameter settings
随机推荐
三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
运动捕捉系统原理
C#聯合halcon應用——大華相機采集類
Richview trvdocparameters page parameter settings
STC 32-bit 8051 single chip microcomputer development example tutorial II i/o working mode and its configuration
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Flask 常用组件
Use of common built-in classes of JS
Penetration tools - trustedsec's penetration testing framework (PTF)
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
ORA-01950
目标检测——Yolo系列
How to turn off the boot auto start software in win11
Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
STC 32-bit 8051 single chip microcomputer development example tutorial three program compilation setting and download
[mysql] install mysql5.7
极客DIY开源方案分享——数字幅频均衡功率放大器设计(实用的嵌入式电子设计作品软硬件综合实践)
Develop those things: easycvr platform adds playback address authentication function
Error in installing sharp
2022年高处安装、维护、拆除考题模拟考试平台操作