-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
26 lines (24 loc) · 822 Bytes
/
solution.py
File metadata and controls
26 lines (24 loc) · 822 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
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
for token in tokens:
if token in ['+', '-', '*', '/']:
value2 = int(stack.pop())
value1 = int(stack.pop())
value = 0
if token == '+':
value = value1 + value2
elif token == '-':
value = value1 - value2
elif token == '*':
value = value1 * value2
elif token == '/':
value = int(value1 * 1.0 / value2)
stack.append(value)
else:
stack.append(token)
return 0 if len(stack) == 0 else int(stack[0])