-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy path0150.py
28 lines (26 loc) · 860 Bytes
/
0150.py
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
28
class Solution:
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = list()
oper = ['+', '-', '*', '/']
for char in tokens:
if char not in oper:
stack.append(int(char))
else:
top1 = stack.pop()
top2 = stack.pop()
if char == '+':
stack.append(top2 + top1)
elif char == '-':
stack.append(top2 - top1)
elif char == '*':
stack.append(top2 * top1)
elif char == '/':
stack.append(int(top2 / top1))
return stack.pop()
if __name__ == "__main__":
s = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
print(Solution().evalRPN(s))