当前位置:网站首页>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 .!
边栏推荐
- Windows installation WSL (II)
- Asp .NetCore 微信订阅号自动回复之文本篇
- 微信小程序缓存过期时间的相关设置(推荐)
- Barbie q! How to analyze the new game app?
- Data analysis methodology and previous experience summary [notes dry goods]
- Use the htaccess file to prohibit the script execution permission in the directory
- Timer和ScheduledThreadPoolExecutor的区别
- Node -- egg creates a local file access interface
- From 20s to 500ms, I used these three methods
- [opencv450] hog+svm and hog+cascade for pedestrian detection
猜你喜欢
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
SQL Server Installation Guide
The origin of usb-if Association and various interfaces
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Example explanation: move graph explorer to jupyterlab
ThreadLocal内存泄漏是什么,怎么解决
牛客-练习赛101-推理小丑
时间复杂度与空间复杂度
Dongge cashes in and the boss retires?
2023 Lexus ES products have been announced, which makes great progress this time
随机推荐
From 20s to 500ms, I used these three methods
Record the accidental success and failure of uploading large files
数据库--SqlServer详解
基于全志H3的QT5.12.9移植教程
What is the purpose of ERP project implementation plan?
【CTF】bjdctf_ 2020_ babystack2
在证券账户上买基金安全吗?哪里可以买基金
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
Promise和模块块化编程
SQL数据分析之子查询的综合用法和案例题【耐心整理】
一个实习生的CnosDB之旅
The origin of usb-if Association and various interfaces
Take the enclave Park as a sample to see how Yuhua and Shaoshan play the song of Chang Zhu Tan integrated development
LeetCode中等题题分享(5)
Operate database transactions with jpatractionmanager
I would like to ask, which securities is better for securities account opening? Is it safe to open a mobile account?
Key points of security agreement
Node——Egg 实现上传文件接口
The new version of graphic network PDF will be released soon
js 公共库 cdn 推荐