-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path150_2.py
More file actions
27 lines (24 loc) · 763 Bytes
/
150_2.py
File metadata and controls
27 lines (24 loc) · 763 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import operator
class Solution:
def evalRPN(self, tokens: List[str]) -> int:
if not tokens:
return 0
oprands = []
operators = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
"/": operator.truediv
}
for token in tokens:
if token in operators:
oprand2 = oprands.pop()
oprand1 = oprands.pop()
op = operators[token]
result = op(oprand1, oprand2)
if token == '/':
result = int(result)
oprands.append(result)
else:
oprands.append(int(token))
return oprands.pop()