当前位置:网站首页>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 .!
边栏推荐
- 使用多线程Callable查询oracle数据库
- LeetCode 0241.为运算表达式设计优先级 - DFS
- What is the purpose of ERP project implementation plan?
- Node - generate wechat permission verification configuration
- 挖财学堂开户打新债安全可靠嘛?
- Material design component - use bottomsheet to show extended content (I)
- leetcode96不同的二叉搜索树
- Talents come from afar, and Wangcheng district has consolidated the intellectual base of "strengthening the provincial capital"
- An intern's journey to cnosdb
- 13 MySQL constraint
猜你喜欢

Asp . Text of automatic reply to NETCORE wechat subscription number

Selectively inhibiting learning bias for active sampling

The origin of usb-if Association and various interfaces

Halcon knowledge: an attempt of 3D reconstruction

Intelligent operation and maintenance practice: banking business process and single transaction tracking

It's nothing to be utilitarian!

EMC circuit protection device for surge and impulse current protection

Correlation - intra group correlation coefficient

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速

PWN attack and defense world cgpwn2
随机推荐
【QT】Qt 使用MSVC2017找不到编译器的解决办法
LeetCode中等题题分享(5)
基于全志H3的QT5.12.9移植教程
Jielizhi, production line assembly link [chapter]
【CTF】bjdctf_ 2020_ babystack2
leetcode96不同的二叉搜索树
Correlation - intra group correlation coefficient
SQL数据分析之子查询的综合用法和案例题【耐心整理】
Data analysis methodology and previous experience summary [notes dry goods]
[QT] solve the problem that QT MSVC 2017 cannot compile
js 公共库 cdn 推荐
Accelerator systems initiative is an independent non-profit organization
股票开户哪个证券公司比较安全
起床困难综合症(按位贪心)
如何提升数据质量
Learn online case practice
13 MySQL constraint
启牛学院开户安全的吗?开户怎么开?
When installing mysql, there are two packages: Perl (data:: dumper) and Perl (JSON)
LeetCode 0241.为运算表达式设计优先级 - DFS