当前位置:网站首页>Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
Leetcode skimming: stack and queue 05 (inverse Polish expression evaluation)
2022-07-02 00:26:00 【Taotao can't learn English】
150. Evaluate the inverse Polish expression
according to Reverse Polish notation , Find the value of the expression .
Valid operators include + , - , * , / . Each operand can be an integer , It can also be another inverse Polish expression .
explain :
Integer division keeps only the integer part .
Given an inverse Polish expression is always valid . let me put it another way , The expression always yields a valid number and does not have a divisor of 0 The situation of .
Example 1:
- Input : [“2”, “1”, “+”, “3”, " * "]
- Output : 9
- explain : This formula is transformed into a common infix arithmetic expression as :((2 + 1) * 3) = 9
Example 2:
- Input : [“4”, “13”, “5”, “/”, “+”]
- Output : 6
- explain : This formula is transformed into a common infix arithmetic expression as :(4 + (13 / 5)) = 6
Example 3:
Input : [“10”, “6”, “9”, “3”, “+”, “-11”, " * ", “/”, " * ", “17”, “+”, “5”, “+”]
Output : 22
explain : This formula is transformed into a common infix arithmetic expression as :
((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
Stack operation : When it comes to numbers, put them on the stack , The first one out of the stack is addend 、 Subtract 、 Divisor 、 The multiplier ; The last one out of the stack is the addend 、 minuend 、 Divisor 、 Multiplier .
When it comes to numbers, it's on the stack 、 When encountering an operator, take out the two numbers at the top of the stack for calculation , Then push the result into the stack .
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 "+":
// Addition number
Integer num1 = Integer.valueOf((String) stack.pop());
// Augend
Integer num2 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num2 + num1));
break;
case "-":
// Subtract
Integer num3 = Integer.valueOf((String) stack.pop());
// minuend
Integer num4 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num4 - num3));
break;
case "*":
// The multiplier
Integer num5 = Integer.valueOf((String) stack.pop());
// Multiplier
Integer num6 = Integer.valueOf((String) stack.pop());
stack.push(String.valueOf(num6 * num5));
break;
case "/":
// Divisor
Integer num7 = Integer.valueOf((String) stack.pop());
// Divisor
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());
}
}

Again AC, comfortable .!
边栏推荐
- 时间复杂度与空间复杂度
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- 【CTF】bjdctf_2020_babystack2
- MySQL: the difference between insert ignore, insert and replace
- EMC circuit protection device for surge and impulse current protection
- Asp .NetCore 微信订阅号自动回复之文本篇
- Selectively inhibiting learning bias for active sampling
- Accelerator systems initiative is an independent non-profit organization
- Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
- 【QT】Qt 使用MSVC2017找不到编译器的解决办法
猜你喜欢

Using multithreaded callable to query Oracle Database

基于全志H3的QT5.12.9移植教程

EMC circuit protection device for surge and impulse current protection

SQL Server 安裝指南

2023款雷克萨斯ES产品公布,这回进步很有感

Digital transformation has a long way to go, so how to take the key first step

Intelligent operation and maintenance practice: banking business process and single transaction tracking
![[opencv450] hog+svm and hog+cascade for pedestrian detection](/img/55/cdf0bb8231ee59e34c8d5a9d6def23.png)
[opencv450] hog+svm and hog+cascade for pedestrian detection

Promise和模块块化编程
![Flow control statement of SQL data analysis [if, case... When detailed]](/img/7b/eabb0700936d34a3a145737580be88.png)
Flow control statement of SQL data analysis [if, case... When detailed]
随机推荐
Using multithreaded callable to query Oracle Database
leetcode96不同的二叉搜索樹
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
[template] adaptive Simpson integral
LDR6035智能蓝牙音响可对手机设备持续充放电方案
LeetCode 0241. Design priority for arithmetic expressions - DFS
B tree and b+tree of MySQL
EMC circuit protection device for surge and impulse current protection
使用htaccess文件禁止目录里的脚本执行权限
记录一下大文件上传偶然成功偶然失败问题
Correlation - intra group correlation coefficient
Jielizhi, production line assembly link [chapter]
SQL Server 安装指南
Accelerator systems initiative is an independent non-profit organization
Node——添加压缩文件
Difficult to get up syndrome (bit by bit greed)
【QT】QtCreator卸载与安装(非正常状态)
Shell process control
Vue force cleaning browser cache
Download the online video m3u8 tutorial