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. Java学习十八

    学习内容: 1.Java集合 1.自定义的set类添加重复数据需要在实体类中添加hashcode和equals方法. 2.查找set对象信息(以宠物猫为例) //在集合中查找花花的信息并输出 if(s ...

  2. iPhone到底能不能充一整夜电?

    其实在国内,手机充电一直是个"玄学问题".早在多年前就有大神向小白敦敦教导,"新买的手机要将电用完,并充12个小时,如此反复三次才能延长手机电池寿命".甚至直到 ...

  3. JavaScript学习总结(七)

    这一讲我们来学习DOM编程(十分重要),有了DOM编程,我们就可以操作任意的HTML元素了. DOM,文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建 ...

  4. Java 面向对象概述原理: 多态、Object类,转型(8)

    Java 面向对象概述原理: 多态.Object类,转型(8) http://docs.oracle.com/javase/tutorial/java/IandI/override.html Java ...

  5. Ubuntu的软件安装管理---dpkg与apt-*详解

    摘要:软件厂商先在他们的系统上面编译好了我们用户所需要的软件,然后将这个编译好并可执行的软件直接发布给用户安装.不同的 Linux 发行版使用不同的打包系统,一般而言,大多数发行版分别属于两大包管理技 ...

  6. GCC常见命令汇总

    int main() { test(); } man.c如上: #include <stdio.h> void test() { printf("test\n"); } ...

  7. RAC,ReactiveSwift

    1.创建信号 // 1.通过信号发生器创建(冷信号) let producer = SignalProducer<String, NoError>.init { (observer, _) ...

  8. l1 和l2范数的真实意义

    很长时间一直没有明白真实的含义,十一期间补充一下这方面的知识. l0 范数是 ||x||0 = xi (xi不等于0)代表非0数字的个数,[1,2,3,4,5]  非0个数为5,[0,1,2,0,3] ...

  9. Python3 Windows服务器简单实现 手机访问

    设备 1. PC with Win10 2. Python 3.5.1 步骤 打开CMD,cd 到e盘,mkdir pythonWebserver 建立文件夹 //在E盘建立文件夹,作为服务器的根目录 ...

  10. KMP算法复杂度证明

    引言 KMP算法应该是看了一次又一次,比赛的时候字符串不是我负责,所以学到的东西又还给网上的博客了-- 退役后再翻开看,看到模板,心想这不是\(O(n^2)\)的复杂度吗? 有两个循环也不能看做是\( ...