当前位置:网站首页>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,舒服。!
边栏推荐
- [Mysql]安装Mysql5.7
- Write blog documents
- 架构师毕业总结
- Flask 常用组件
- 目標檢測——Yolo系列
- Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
- Entering Ruxin Town, digital intelligence transformation connects "future community"
- 【Leetcode】最大连续1的个数
- deb文件安装
- Develop those things: easycvr platform adds playback address authentication function
猜你喜欢

Redis installation and startup in Windows environment (background startup)

小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?

合成大西瓜小游戏微信小程序源码/微信游戏小程序源码

新牛牛盲盒微信小程序源码_支持流量变现,带完整素材图片

薛定谔的日语学习小程序源码

随机头像大全,多分类带历史记录微信小程序源码_支持流量主

EDA工具对芯片产业的重要性知识科普

fastDFS入门

1592 example 1 King (sgu223 loj10170 luogu1896 increase + / provincial election -) violent thinking pressure DP 01 Backpack

PLC模拟量输入 模拟量转换FB S_ITR(三菱FX3U)
随机推荐
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
2022安全员-B证考试练习题模拟考试平台操作
Target detection - Yolo series
EDA工具对芯片产业的重要性知识科普
【let var const】
[Mysql]安装Mysql5.7
300 linear algebra Lecture 4 linear equations
小鸟逃票登机,如何反思,应如何解决,飞机为何怕小鸟?
强大的万年历微信小程序源码-支持多做流量主模式
基于图的 Affinity Propagation 聚类计算公式详解和代码示例
How to connect the two nodes of the flow chart
300题线性代数 第四讲 线性方程组
How to create a pyramid with openmesh
图片拼图微信小程序源码_支持多模板制作和流量主
Realize pyramids through JS (asterisk pyramid, palindrome symmetric digital pyramid)
Use of common built-in classes of JS
docker ubuntu容器中安装mysql遇到的问题
[C language] explain the usage of memset() function in detail
Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
数据分析师听起来很高大上?了解这几点你再决定是否转型