leetcode150 Evaluate Reverse Polish Notation
2016-01-19 19:04
316 查看
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
[“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9
[“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6
Subscribe to see which companies asked this question
python
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
[“2”, “1”, “+”, “3”, ““] -> ((2 + 1) 3) -> 9
[“4”, “13”, “5”, “/”, “+”] -> (4 + (13 / 5)) -> 6
Subscribe to see which companies asked this question
python
class Solution(object): def isOp(self,s): return s=="+" or s=="-" or s=="*" or s=="/" def add(self,op1,op2): return op1+op2 def sub(self,op1,op2): return op1-op2 def mul(self,op1,op2): return op1 * op2 def div(self,op1,op2): if op1*op2<0: return -((-op1)/op2) return op1/op2 operator = {"+":add,"-":sub,"*":mul,"/":div} def evalRPN(self,tokens): nums=[] for s in tokens: if self.isOp(s): op2 = nums.pop() op1 = nums.pop() re = self.operator.get(s)(self,op1,op2) nums.append(re) else: nums.append(int(s)) return nums[0]
相关文章推荐
- 编译lua学习代码
- 四、lua常用小知识
- lua中获取时间
- 快速掌握Lua 5.3 —— 编译,运行以及错误
- sublime Text2编译lua(windows平台)
- 三、lua coroutine
- Lua 语句
- [leetcode] 150. Evaluate Reverse Polish Notation 解题报告
- *Evaluate Reverse Polish Notation
- 简单谈谈lua和c的交互
- 快速掌握Lua 5.3 —— 各种变量和值
- lua排序算法
- 快速掌握Lua 5.3 —— "Iterators"和"Generic for"
- 快速掌握Lua 5.3 —— 函数
- Lua中的table函数库
- lua和c的亲密接触
- sicily 递归练习 1005. Arithmetic Expression Evaluation
- lua和c的亲密接触
- lua BaseClass
- lua 怎么写两个字符串相加