当前位置:网站首页>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 .!
边栏推荐
- 13 MySQL constraint
- Digital transformation has a long way to go, so how to take the key first step
- Node——生成微信权限验证配置
- Use es to realize epidemic map or take out order function (including code and data)
- Use the htaccess file to prohibit the script execution permission in the directory
- [cascade classifier training parameters] training Haar cascades
- UVM tutorial
- [QT] QT cannot find a solution to the compiler using msvc2017
- Leetcode96 different binary search trees
- 【CTF】bjdctf_ 2020_ babystack2
猜你喜欢

Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results

SQL Server 安裝指南

It's nothing to be utilitarian!

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

Review data desensitization system

Niuke - Practice 101 - reasoning clown

leetcode96不同的二叉搜索樹
![Data analysis methodology and previous experience summary [notes dry goods]](/img/00/e4c4cf37f1ca9134546f970d800226.png)
Data analysis methodology and previous experience summary [notes dry goods]

4. Object mapping Mapstercover
![[QT] QT cannot find a solution to the compiler using msvc2017](/img/80/a4b17d6cc1ab6fe1366a3a3f43ec2e.png)
[QT] QT cannot find a solution to the compiler using msvc2017
随机推荐
Node -- add compressed file
Use es to realize epidemic map or take out order function (including code and data)
Material design component - use bottomsheet to show extended content (I)
求逆序数的三个方法
Halcon knowledge: an attempt of 3D reconstruction
Example explanation: move graph explorer to jupyterlab
ERP项目施行计划的目的是什么?
The difference between timer and scheduledthreadpoolexecutor
Operate database transactions with jpatractionmanager
SQL Server Installation Guide
【opencv】train&test HOG+SVM
The origin of usb-if Association and various interfaces
使用多线程Callable查询oracle数据库
下载在线视频 m3u8使用教程
Selectively inhibiting learning bias for active sampling
[CTF] bjdctf 2020 Bar _ Bacystack2
[template] adaptive Simpson integral
SQL Server 安装指南
Flow control statement of SQL data analysis [if, case... When detailed]
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)