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
while, why did not I think of using stack in the first place?
Here is the improvement!
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(item)
else:
num2 = int(stack.pop())
num1 = int(stack.pop())
if item == '+':
stack.append(str(num1 + num2))
elif item == '-':
stack.append(str(num1 - num2))
elif item == '*':
stack.append(str(num1 * num2))
elif item == '/':
if num1 * num2 < 0:
stack.append(str(- (abs(num1) / abs(num2))))
else:
stack.append(str(num1 / num2))
return int(stack[0])