150. Evaluate Reverse Polish Notation 逆波兰表达式求值
【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)
Section titled “【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)”标签: LeetCode
题目地址:https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
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后缀表达式转中缀表达式,并且求值。
python 有个牛逼的函数,就是eval(),可以给它一个运算表达式,直接给你求值。中缀表达式转正常表达式很简单了,直接用栈就行。
但是!!需要注意的是,python中的’/‘负数除法和c语言不太一样。在python中,(-1)/2=-1,而在c语言中,(-1)/2=0。也就是c语言中,除法是向零取整,即舍弃小数点后的数。而在python中,是向下取整的。而这道题的oj是默认的c语言中的语法,所以需要在遇到’/‘的时候注意一下。
一种方式是采用负负得正的方法,用两个负号变成整数的除法,再取负。
另一种方式是使用operator.truediv(int(a), int(b))变成和c相同的方式。
class Solution(object): def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ stack = [] operators = ['+', '-', '*', '/'] for token in tokens: if token not in operators: stack.append(token) else: b = stack.pop() a = stack.pop() if token == '/' and int(a) * int(b) < 0: res = eval('-' + '(' + '-' + a + '/' + b + ')') else: res = eval(a + token + b) stack.append(str(res)) return int(stack.pop())或者:
class Solution(object): def evalRPN(self, tokens): """ :type tokens: List[str] :rtype: int """ stack = [] operators = ['+', '-', '*', '/'] for token in tokens: if token not in operators: stack.append(token) else: b = stack.pop() a = stack.pop() if token == '/': res = int(operator.truediv(int(a), int(b))) else: res = eval(a + token + b) stack.append(str(res)) return int(stack.pop())2018 年 3 月 14 日

评论与交流