class Solution: # @param tokens, a list of string # @return an integer def evalRPN(self, tokens): stack = [] for i in tokens: if i not in ('+', '-', '*', '/'): stack.append(int(i)) else: op2 = stack.pop() op1 = stack.pop() if i == '+': stack.append(op1 + op2) elif i == '-': stack.append(op1 - op2) elif i == '*': stack.append(op1 * op2) else: stack.append(int(op1 * 1.0 / op2)) return stack[0]
Thursday, September 18, 2014
Leetcode: Evaluate Reverse Polish Notation @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment