Skip to content

Commit 1b222d7

Browse files
authored
Merge pull request doocs#107 from ashwek/evalRPN
0150 Evaluate Reverse Polish Notation - Python
2 parents e4f0d1d + 4ad52d7 commit 1b222d7

File tree

1 file changed

+18
-0
lines changed
  • solution/0150.Evaluate Reverse Polish Notation

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import operator as op
2+
3+
class Solution:
4+
def evalRPN(self, tokens):
5+
"""
6+
:type tokens: list[str]
7+
:rtype: int
8+
"""
9+
stack = []
10+
Opr = {"+":op.add, "-":op.sub, "*":op.mul, "/":op.truediv}
11+
12+
for i in tokens:
13+
if i in Opr:
14+
stack.append( int( Opr[i](stack.pop(-2), stack.pop(-1)) ) )
15+
else:
16+
stack.append(int(i))
17+
18+
return stack[0]

0 commit comments

Comments
 (0)