当前位置:网站首页>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,舒服。!
边栏推荐
- Hls4ml reports an error the board_ part definition was not found for tul. com. tw:pynq-z2:part0:1.0.
- 三菱PLC FX3U脉冲轴点动功能块(MC_Jog)
- PHP gets the external chain address of wechat applet and applet store
- C#联合halcon应用——大华相机采集类
- Big factories are wolves, small factories are dogs?
- On the next generation entrance of the metauniverse -- the implementation of brain computer interface
- internship:逐渐迈向项目开发
- On the usage of a magic function
- 喜马拉雅自研网关架构演进过程
- Uniapp uses Tencent map to select points without window monitoring to return users' location information. How to deal with it
猜你喜欢
EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
Practical project notes (I) -- creation of virtual machine
【多线程】 实现单例模式 ( 饿汉、懒汉 ) 实现线程安全的单例模式 (双重效验锁)
Items in richview documents
Entering Ruxin Town, digital intelligence transformation connects "future community"
Solve the problem of slow or failed vscode download
小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?
王者战力查询改名工具箱小程序源码-带流量主激励广告
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
3D panoramic model display visualization technology demonstration
随机推荐
寫博客文檔
C # joint halcon Application - Dahua Camera Collection class
Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
升级版手机检测微信工具小程序源码-支持多种流量主模式
人脸识别系统 —— OpenCV人脸检测
收藏:存储知识全面总结
deb文件安装
Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
C#联合halcon应用——大华相机采集类
Principle of motion capture system
《软件工程导论(第六版)》 张海藩 复习笔记
【Leetcode】最大连续1的个数
Richview trvdocparameters page parameter settings
Learn white box test case design from simple to deep
Develop those things: easycvr platform adds playback address authentication function
Use Zadig to build a continuous delivery platform from 0 to 1
Detailed explanation and code example of affinity propagation clustering calculation formula based on graph
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
How to connect the two nodes of the flow chart