当前位置:网站首页>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 .!
边栏推荐
- 二叉搜索树的创建,查找,添加,删除操作
- BPR (Bayesian personalized sorting)
- Is the securities account given by qiniu business school safe? Where can I open an account
- Node - generate wechat permission verification configuration
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- 2023 Lexus ES products have been announced, which makes great progress this time
- Windows10 install WSL (I) (wslregisterdistribution error)
- Database -- sqlserver details
- 13 MySQL constraint
- Leetcode 96 différents arbres de recherche binaires
猜你喜欢
【QT】Qt 使用MSVC2017找不到编译器的解决办法
Niuke - Practice 101 - reasoning clown
[QT] QT cannot find a solution to the compiler using msvc2017
B tree and b+tree of MySQL
PWN attack and defense world cgpwn2
SQL数据分析之流程控制语句【if,case...when详解】
Graduation season is both a farewell and a new beginning
Practical calculation of the whole process of operational amplifier hysteresis comparator
Download the online video m3u8 tutorial
Export default the exported object cannot be deconstructed, and module Differences between exports
随机推荐
Linux CentOS7安装Oracle11g的超完美新手教程
使用 ES 实现疫情地图或者外卖点餐功能(含代码及数据)
Heketi record
Node - generate wechat permission verification configuration
数据库--SqlServer详解
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
Graduation season is both a farewell and a new beginning
leetcode96不同的二叉搜索樹
Use the htaccess file to prohibit the script execution permission in the directory
Shell custom function
heketi 记录
Qt5.12.9 migration tutorial based on Quanzhi H3
Jielizhi Bluetooth headset quality control and production skills [chapter]
export default 导出的对象,不能解构问题,和module.exports的区别
449-原码、补码、反码
回顾数据脱敏系统
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Digital transformation has a long way to go, so how to take the key first step
How to improve data quality
[QT] QT cannot find a solution to the compiler using msvc2017