当前位置:网站首页>Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
Simulation volume leetcode [general] 150. evaluation of inverse Polish expression
2022-07-29 06:56:00 【Encounter simulation volume】
Summary : Simulation volume Leetcode Summary of questions
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 .
Be careful The division between two integers retains only the integer part .
It can be guaranteed that the given 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 :tokens = [“2”,“1”,“+”,“3”,“*”]
Output :9
explain : This formula is transformed into a common infix arithmetic expression as :((2 + 1) * 3) = 9
Example 2:
Input :tokens = [“4”,“13”,“5”,“/”,“+”]
Output :6
explain : This formula is transformed into a common infix arithmetic expression as :(4 + (13 / 5)) = 6
Example 3:
Input :tokens = [“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
Tips :
1 <= tokens.length <= 104
tokens[i] It's an operator (“+”、“-”、“*” or “/”), Or in the range [-200, 200] An integer in
Reverse Polish notation :
The inverse Polish expression is a suffix expression , The so-called suffix means that the operator is written after .
The usual formula is a infix expression , Such as ( 1 + 2 ) * ( 3 + 4 ) .
The inverse Polish expression of the formula is written as ( ( 1 2 + ) ( 3 4 + ) * ) .
The inverse Polish expression has two main advantages :
There is no ambiguity in the expression after removing the brackets , Even if the above formula is written as 1 2 + 3 4 + * You can also calculate the correct result according to the order .
It is suitable for stack operation : When it comes to numbers, it's on the stack ; When you encounter an operator, you take out two numbers at the top of the stack to calculate , And push the results into the stack
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/evaluate-reverse-polish-notation
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
from leetcode_python.utils import *
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
for x in tokens:
if x in '+-*/':
b = stack.pop(-1)
a = stack.pop(-1)
stack.append(int(eval(f"{
a}{
x}{
b}")))
else:
stack.append(x)
return int(stack[0])
def test(data_test):
s = Solution()
data = data_test # normal
# data = [List2Node(data_test[0])] # list turn node
return s.evalRPN(*data)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[["10","6","9","3","+","-11","*","/","*","17","+","5","+"]],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- MySQL:当你CRUD时BufferPool中发生了什么?十张图就能说清楚
- Best example of amortized cost
- Teacher wangshuyao's notes on operations research 06 linear programming and simplex method (geometric significance)
- 模拟卷Leetcode【普通】222. 完全二叉树的节点个数
- Teacher wangshuyao's notes on operations research 03 KKT theorem
- Hongke share | let you have a comprehensive understanding of "can bus errors" (IV) -- producing and recording can errors in practice
- Computer right mouse click always turn around what's going on
- Share some tips for better code, smooth coding and improve efficiency
- 2022年SQL经典面试题总结(带解析)
- 没那么简单的单例模式
猜你喜欢

剑指 Offer II 115:重建序列

SDN topology discovery principle

CNN convolutional neural network

基于Matlab解决线性规划问题

【经验】通过跳板机远程连接内网服务器的相关配置

Let the computer run only one program setting

Software definition boundary SDP

实战!聊聊如何解决MySQL深分页问题

王树尧老师运筹学课程笔记 07 线性规划与单纯形法(标准型、基、基解、基可行解、可行基)

LDAP brief description and unified authentication description
随机推荐
【冷冻电镜|论文阅读】emClarity:用于高分辨率冷冻电子断层扫描和子断层平均的软件
Salesforce中过滤器Filter使用的相对日期
吴恩达老师机器学习课程笔记 05 Octave教程
关于SQL Server语句入门级应用阶段性学习——找工作必备(一)
模拟卷Leetcode【普通】222. 完全二叉树的节点个数
王树尧老师运筹学课程笔记 05 线性规划与单纯形法(概念、建模、标准型)
Instruction rearrangement under multithreading concurrency
【论文阅读 | cryoET】Gum-Net:快速准确的3D Subtomo图像对齐和平均的无监督几何匹配
【论文阅读 | 冷冻电镜】RELION 4.0 中新的 subtomogram averaging 方法解读
Teacher wangshuyao's notes on operations research 04 fundamentals of linear algebra
会话推荐中的价格偏好和兴趣偏好共同建模-论文泛读
【冷冻电镜|论文阅读】A feature-guided, focused 3D signal permutation method for subtomogram averaging
【flask入门系列】Flask-SQLAlchemy的安装与配置
数据库系统概述
C语言数据类型
Recurrent neural network RNN
模拟卷Leetcode【普通】150. 逆波兰表达式求值
Actual combat! Talk about how to solve the deep paging problem of MySQL
王树尧老师运筹学课程笔记 10 线性规划与单纯形法(关于检测数与退化的讨论)
Introduction to OSPF theory