LeetCode-Evaluate Reverse Polish Notation (Python)
来源:互联网 发布:淘宝网店手机可以看吗 编辑:程序博客网 时间:2024/05/18 00:10
【问题】
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
【代码】
class Solution: # @param tokens, a list of string # @return an integer def evalRPN(self, tokens): stack = [] for item in tokens: if item not in ("+", "-", "*", "/"): stack.append(int(item)) else: op2 = stack.pop() op1 = stack.pop() if item == "+": stack.append(op1 + op2) elif item == "-": stack.append(op1 - op2) elif item == "*": stack.append(op1 * op2) elif item == "/": stack.append(int(op1 *1.0 / op2)) return stack[0]
0 0
- 【LeetCode with Python】 Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation (Python)
- 【leetcode】:Evaluate Reverse Polish Notation (python)
- Evaluate Reverse Polish Notation Leetcode Python
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- libevent源码浅析: http库
- java中inputstream的.read()函数的三种用法
- LINUX下查看系统参数的常见命令
- 计算几何概述
- android Bluetooth连接方式 SCO & ACL
- LeetCode-Evaluate Reverse Polish Notation (Python)
- Excel中将姓名与手机号中的字符替换成星号*
- LINUX下搭建FTP服务器
- Android中的Handler, Looper, MessageQueue和Thread
- 黑马程序员.Android攻城狮.JAVA基础.1.3.Java面向对象
- 以后就在家做做面包,带带孩子吧
- 二分图带权匹配 KM算法与费用流模型建立
- hbase架构介绍
- Aizu 2249 单源最短路变形 spfa模板改写