Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Example 2:

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Example 3:

Input: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
Output: 22
Explanation:
((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
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
stack = []
import string
nums = string.digits
operator = ['+', '-', '*', '/'] for token in tokens:
if token in operator:
last_one = stack.pop()
last_two = stack.pop()
cur_val = self.helper(last_one, last_two, token)
stack.append(cur_val)
else:
stack.append(int(token))
return stack.pop() def helper(self, last_one, last_two, operator):
if operator == '+':
return last_two + last_one
elif operator == '-':
return last_two - last_one
elif operator == '*':
return last_two * last_one
elif operator == '/':
# special case to get ceiling value
if last_two * last_one < 0 and last_two % last_one != 0:
return last_two // last_one + 1
else:
return last_two // last_one
else:
return

[LC] 150. Evaluate Reverse Polish Notation的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 150. Evaluate Reverse Polish Notation - LeetCode

    Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...

  3. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  4. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  5. 150. Evaluate Reverse Polish Notation逆波兰表达式

    [抄题]: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are ...

  6. LeetCode OJ 150. Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  8. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

随机推荐

  1. MVC——EF 回顾总结

    回顾一下MVC的知识点. 其实开始 我在学校的知识对MVC 还是很模糊的一个概念.只是记得结合EasyUI 增删改查 和分页,代码都是模糊的 进过这段时间的学习,让我对MVC 有了一个很清楚的认识. ...

  2. 网页时不时打不开?试试阿里DNS 233.5.5.5 /233.6..6.6

    最经上网都是用手机热点,但发现用谷歌浏览器时,时不时打不开网页.最后发现是DNS的问题,原来我的dns是8.8.8.8. 最后更改成阿里的DNS 233.5.5.5 /233.6..6.6,打开网页流 ...

  3. UML-如何创建领域模型?

    以当前迭代中所要设计的需求为界: 1.寻找概念类 2.将其绘制为类图 3.添加关联和属性

  4. Powershell 中的管道

    管道 上个命令中的输出,通过管道作为下个命令的输入.Linux中的管道传递的是text,但ps中传递的是object.但是命令究竟返回的是什么类型呢?以下命令回答了这个问题: get-service ...

  5. memcached redis 本质区别是功能多少

    功能: 1.memcached 数据类型比较单一,数据淘汰策略单一,功能简单 2.redis 数据类型比较全面, 数据淘汰策略比较多,功能较强 有持久化能力,可以持久存储少量数据(数据量不会大于本机内 ...

  6. SVN的import和export的使用

    import 上传到服务器 export 下载到本地 import这个命令在使用的时候,都要多加一级目录 比如 目录结构是 E:\\1\\2  在1目录下执行import  只会把2目录提交上去,1的 ...

  7. MySQL数据库数据迁移:从一个服务器到另一个服务器

    需要两个服务器数据库版本相同才可迁移 1:单个或多个数据库 mysqldump -h远程ip -u用户 -p密码 -P3306 -- -uroot -p -P3306 执行后输入本地数据库密码即可 : ...

  8. Java集合详解(全)

    Java的集合主要有List , Set, Map List , Set继承至Collection接口,Map为独立接口 List下有ArrayList,LinkedList,Vector Set下有 ...

  9. 嵌入式c语言编码规范

    学习嵌入式的同学应该首先掌握嵌入式编码规范,这样才能更好的嵌入式系统. 下面就从这几个方面讲解一下嵌入式c编码规范. 注释风格.排版风格.头文件风格.变量定义.宏定义.函数 1 注释风格 1.1  注 ...

  10. python基础——散列类型

    集合 集合具有不重复性,无序性的可变对象. 集合定义 直接定义 如:a = {'a','b',2} 别的类型转换,利用set    a = set(b) 其中b可以是一个列表或字符串等 增 add   ...