Skip to content

Commit 125a060

Browse files
Question 150
1 parent 59fa564 commit 125a060

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

150. Evaluate Reverse Polish Notation.py

+25
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@
3131
= 22
3232
'''
3333

34+
class Solution:
35+
def evalRPN(self, arr: List[str]) -> int:
36+
stack = []
37+
for i in range(len(arr)):
38+
if arr[i]=='+':
39+
num1 = stack.pop()
40+
num2 = stack.pop()
41+
stack.append(num2+num1)
42+
elif arr[i]=='-':
43+
num1 = stack.pop()
44+
num2 = stack.pop()
45+
stack.append(num2-num1)
46+
elif arr[i]=='*':
47+
num1 = stack.pop()
48+
num2 = stack.pop()
49+
stack.append(num2*num1)
50+
elif arr[i]=='/':
51+
num1 = stack.pop()
52+
num2 = stack.pop()
53+
stack.append(int(num2/num1))
54+
else:
55+
stack.append(int(arr[i]))
56+
# print(stack)
57+
return stack[0]
58+

0 commit comments

Comments
 (0)