Skip to content

Commit 4bc6b51

Browse files
committed
add 150 stack
1 parent b885294 commit 4bc6b51

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

LeetCodeSolutions/150.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#==================================================
2+
#==> Title:
3+
#==> Author: Zhang zhen
4+
#==> Email: hustmatnoble.gmail.com
5+
#==> GitHub: https://github.com/MatNoble
6+
#==> Date:
7+
#==================================================
8+
9+
from typing import List
10+
class Solution:
11+
def evalRPN(self, tokens: List[str]) -> int:
12+
n = len(tokens)
13+
# if n < 3: return False
14+
i = res = 0
15+
stack = []
16+
while i<n:
17+
if tokens[i] == '+':
18+
tmp = stack.pop()
19+
res = stack.pop() + tmp
20+
elif tokens[i] == '-':
21+
tmp = stack.pop()
22+
res = stack.pop() - tmp
23+
elif tokens[i] == '*':
24+
tmp = stack.pop()
25+
res = stack.pop() * tmp
26+
elif tokens[i] == '/':
27+
tmp = stack.pop()
28+
res = int(stack.pop() / tmp)
29+
else:
30+
res = int(tokens[i])
31+
stack.append(res)
32+
i += 1
33+
return stack[0]
34+
35+
mat = Solution()
36+
tokens = ["2", "1", "+", "3", "*"]
37+
# tokens = ["4", "13", "5", "/", "+"]
38+
# tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
39+
tokens = ["18"]
40+
mat.evalRPN(tokens)

0 commit comments

Comments
 (0)